为什么我的应用程序调用"setupActionBarWithNavController(navController,appBarConfiguration)"时崩溃? [英] Why does my app crash invoking "setupActionBarWithNavController(navController, appBarConfiguration)"

查看:1765
本文介绍了为什么我的应用程序调用"setupActionBarWithNavController(navController,appBarConfiguration)"时崩溃?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Android Studio的LogCat窗口中收到一条错误消息;

I am getting an error message in the LogCat window of Android Studio;

Process: com.riverstonetech.gositeuk, PID: 27370
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.riverstonetech.gositeuk/com.riverstonetech.gositeuk.CountriesActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void androidx.appcompat.app.ActionBar.setTitle(java.lang.CharSequence)' on a null object reference
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2665)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)
        at android.app.ActivityThread.-wrap12(ActivityThread.java)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:154)
        at android.app.ActivityThread.main(ActivityThread.java:6119)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
     Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void androidx.appcompat.app.ActionBar.setTitle(java.lang.CharSequence)' on a null object reference
        at androidx.navigation.ui.ActionBarOnDestinationChangedListener.setTitle(ActionBarOnDestinationChangedListener.java:48)
        at androidx.navigation.ui.AbstractAppBarOnDestinationChangedListener.onDestinationChanged(AbstractAppBarOnDestinationChangedListener.java:100)
        at androidx.navigation.NavController.addOnDestinationChangedListener(NavController.java:218)
        at androidx.navigation.ui.NavigationUI.setupActionBarWithNavController(NavigationUI.java:220)
        at androidx.navigation.ui.ActivityKt.setupActionBarWithNavController(Activity.kt:74)
        at com.riverstonetech.gositeuk.CountriesActivity.onCreate(CountriesActivity.kt:31)
        at android.app.Activity.performCreate(Activity.java:6679)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2618)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726) 
        at android.app.ActivityThread.-wrap12(ActivityThread.java) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477) 
        at android.os.Handler.dispatchMessage(Handler.java:102) 
        at android.os.Looper.loop(Looper.java:154) 
        at android.app.ActivityThread.main(ActivityThread.java:6119) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)

当我运行包含以下代码的应用程序时:

when I run my app containing the following code:

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_countries)

    val navView: BottomNavigationView = findViewById(R.id.nav_view)

    val navController = findNavController(R.id.nav_host_fragment)
    // Passing each menu ID as a set of Ids because each
    // menu should be considered as top level destinations.

    val appBarConfiguration = AppBarConfiguration(
        setOf(
            R.id.englandFragment, R.id.scotlandFragment, R.id.walesFragment, R.id.nirelandFragment
        )
    )

    setupActionBarWithNavController(navController, appBarConfiguration)

    navView.setupWithNavController(navController)

}

应用到达行时崩溃;

    setupActionBarWithNavController(navController, appBarConfiguration)

推荐答案

听起来好像您为Activity设置了一个主题,该主题具有.NoActionBar.

Sounds like you have a theme set for your Activity that has .NoActionBar.

使用操作栏的更安全解决方案是将Toolbar用作布局的一部分,然后使用 setupWithNavController(Toolbar toolbar, NavController navController, AppBarConfiguration configuration)

A safer solution to using the action bar is to use a Toolbar as part of your layout, then use setupWithNavController(Toolbar toolbar, NavController navController, AppBarConfiguration configuration)

如果您确实想使用操作栏,则您的活动"需要一个提供ActionBar的主题.检查您的AndroidManifest.xml和styles.xml.

If you do want to use an action bar, then your Activity needs a theme that provides an ActionBar. Check your AndroidManifest.xml and styles.xml.

这篇关于为什么我的应用程序调用"setupActionBarWithNavController(navController,appBarConfiguration)"时崩溃?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆