0

我正在我的应用程序中实现一个导航抽屉,我想更改字体。下面是我的 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);



}

这是我的日志猫

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
4

3 回答 3

0

logcat 发现了 NullPointerException,因此很可能字体未正确加载。你能确认你robotomed.tff确实在/assets你的项目中吗?

在这里查看有关资产的更多信息。

于 2013-06-23T02:44:59.583 回答
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"
    ...
于 2021-03-12T20:35:15.093 回答
0
Typeface tf = Typeface.createFromAsset(getBaseContext().getAssets(),
            "fonts/robotomed.ttf");

//getBaseContext() 和 MainActivity.this 一样

我建议创建资产文件夹并在其中添加一个字体目录。您从您的活动中调用资产文件夹,从您的字体目录中调用您的字体。此外,根据 xml 文件的结构,您可能无法使用 findViewById()。原因是您的 TextView 可能是 ListAdapter 的一部分(想想列表项)。话虽如此,如果它适用于 ListAdapter,您将无法调用该 Id。

于 2015-09-09T11:25:51.280 回答