为什么我的应用程序崩溃调用“setupActionBarWithNavController(navController, appBarConfiguration)”

5 android nullpointerexception kotlin android-actionbar android-architecture-navigation

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

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)
Run Code Online (Sandbox Code Playgroud)

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

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)

}
Run Code Online (Sandbox Code Playgroud)

应用程序在到达线路时崩溃;

    setupActionBarWithNavController(navController, appBarConfiguration)

Run Code Online (Sandbox Code Playgroud)

Epi*_*rce 10

听起来您为您的 Activity 设置了一个主题,其中包含 .NoActionBar.

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

如果您确实想使用操作栏,那么您的 Activity 需要一个提供 ActionBar 的主题。检查您的AndroidManifest.xml 和styles.xml。


Din*_*nny 5

如果您不想使用操作栏。删除下面的代码

setupActionBarWithNavController(navController, appBarConfiguration)
Run Code Online (Sandbox Code Playgroud)

或者将 NoActionBar 更改为 DarkActionBar 或任何其他。