2013-06-23 94 views
0

我正在實現一個導航抽屜進入我的應用程序,我想更改字體。下面是我的onCreate方法,我改變了導航抽屜時,應用程序運行崩潰瞬間,我無法弄清楚什麼是錯的問題的字體Android - 更改抽屜式字體/文本

代碼:

public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.navdrawer); 

    android.support.v4.app.FragmentManager fragmentManager = getSupportFragmentManager(); 
    android.support.v4.app.FragmentTransaction fragmentTransaction = fragmentManager 
      .beginTransaction(); 
    Home fragment = new Home(); 
    fragmentTransaction.replace(R.id.llhome, fragment); 
    fragmentTransaction.commit(); 

    Typeface tf = Typeface.createFromAsset(getAssets(), 
      "robotomed.ttf"); 
    TextView tv = (TextView) findViewById(R.id.tvmenu); 
    tv.setTypeface(tf); 

    mPlanetTitles = getResources().getStringArray(R.array.planets_array); 
    mDrawerList = (ListView) findViewById(R.id.left_drawer); 

    mDrawerList.setAdapter(new ArrayAdapter<String>(this, 
      R.layout.drawer_list_item, mPlanetTitles)); 
    // Set the list's click listener 

    mDrawerList.setOnItemClickListener(new DrawerItemClickListener()); 

    mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout); 
    mDrawerToggle = new ActionBarDrawerToggle(this, /* host Activity */ 
    mDrawerLayout, /* DrawerLayout object */ 
    R.drawable.ic_drawer, /* nav drawer icon to replace 'Up' caret */ 
    R.string.drawer_open, /* "open drawer" description */ 
    R.string.drawer_close /* "close drawer" description */ 
    ) { 

     /** Called when a drawer has settled in a completely closed state. */ 
     public void onDrawerClosed(View view) { 
      getActionBar().setTitle("Home"); 
     } 

     /** Called when a drawer has settled in a completely open state. */ 
     public void onDrawerOpened(View drawerView) { 
      getActionBar().setTitle("StayHealthy"); 
     } 
    }; 

    // Set the drawer toggle as the DrawerListener 
    mDrawerLayout.setDrawerListener(mDrawerToggle); 

    getActionBar().setDisplayHomeAsUpEnabled(true); 
    getActionBar().setHomeButtonEnabled(true); 



} 

這裏是我的logcat的

06-22 21:44:21.562: E/AndroidRuntime(32042): FATAL EXCEPTION: main 
06-22 21:44:21.562: E/AndroidRuntime(32042): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.OptimusApps.stayhealthy/com.OptimusApps.stayhealthy.MainActivity}: java.lang.NullPointerException 
06-22 21:44:21.562: E/AndroidRuntime(32042): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2079) 
06-22 21:44:21.562: E/AndroidRuntime(32042): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2104) 
06-22 21:44:21.562: E/AndroidRuntime(32042): at android.app.ActivityThread.access$600(ActivityThread.java:132) 
06-22 21:44:21.562: E/AndroidRuntime(32042): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1157) 
06-22 21:44:21.562: E/AndroidRuntime(32042): at android.os.Handler.dispatchMessage(Handler.java:99) 
06-22 21:44:21.562: E/AndroidRuntime(32042): at android.os.Looper.loop(Looper.java:137) 
06-22 21:44:21.562: E/AndroidRuntime(32042): at android.app.ActivityThread.main(ActivityThread.java:4575) 
06-22 21:44:21.562: E/AndroidRuntime(32042): at java.lang.reflect.Method.invokeNative(Native Method) 
06-22 21:44:21.562: E/AndroidRuntime(32042): at java.lang.reflect.Method.invoke(Method.java:511) 
06-22 21:44:21.562: E/AndroidRuntime(32042): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789) 
06-22 21:44:21.562: E/AndroidRuntime(32042): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:556) 
06-22 21:44:21.562: E/AndroidRuntime(32042): at dalvik.system.NativeStart.main(Native Method) 
06-22 21:44:21.562: E/AndroidRuntime(32042): Caused by: java.lang.NullPointerException 
06-22 21:44:21.562: E/AndroidRuntime(32042): at com.OptimusApps.stayhealthy.MainActivity.onCreate(MainActivity.java:40) 
06-22 21:44:21.562: E/AndroidRuntime(32042): at android.app.Activity.performCreate(Activity.java:4465) 
06-22 21:44:21.562: E/AndroidRuntime(32042): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049) 
06-22 21:44:21.562: E/AndroidRuntime(32042): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2033) 
06-22 21:44:21.562: E/AndroidRuntime(32042): ... 11 more 

回答

0

logcat發現一個NullPointerException,所以很可能是字體加載不正確。你能證實你的robotomed.tff確實在你項目的/assets中嗎?

檢查here瞭解關於資產的更多信息。

0
Typeface tf = Typeface.createFromAsset(getBaseContext().getAssets(), 
      "fonts/robotomed.ttf"); 

// getBaseContext()是MainActivity.this

我建議創建資產的文件夾並添加它的fonts目錄同樣的事情。您可以從您的活動和您的字體目錄中調用資產文件夾。另外,根據您的xml文件的結構,您可能無法使用findViewById()。原因是你的TextView可能是ListAdapter(思考列表項)的一部分。這就是說,如果它適用於ListAdapter,你將無法調用該Id。