我正在我的应用中实现导航抽屉,我想更改字体。下面是我的oncreate方法,我更改导航抽屉的字体问题是当应用程序运行时它立即崩溃我无法弄清楚出了什么问题
代码:
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 :(得分:0)
logcat发现了NullPointerException,因此很可能没有正确加载字体。您可以验证您的robotomed.tff
确实在项目的/assets
中吗?
检查here以获取有关资产的更多信息。
答案 1 :(得分:0)
只需将 app:itemTextAppearance="@font/poppins"
添加到 NavigationView
,如下所示:
<com.google.android.material.navigation.NavigationView
android:id="@+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:itemTextAppearance="@font/poppins"
...