Why does my app crash invoking “setupActionBarWithNavController(navController, appBarConfiguration)”

允我心安 提交于 2020-05-30 19:13:46

问题


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)

}

The app crashes when it reaches the line;

    setupActionBarWithNavController(navController, appBarConfiguration)


回答1:


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

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)

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.




回答2:


I had the same issue and solved the problem. I hope this helps. The problem was I did not add dependencies for fragment library.

  1. Add fragment dependencies on app level build.gradle. implementation 'androidx.fragment:fragment:1.2.4' implementation 'androidx.fragment:fragment-ktx:1.2.4'

  2. Clean build (Build -> Clean Project)

  3. Invalidate Caches and Restart (if developer PC OS is Windows)

  4. Reboot PC (I do not know why. but if I don't reboot PC, the problem occurs again. )

  5. After reboot, build and run the app again.




回答3:


If you don't want to use the action bar. Remove the below code

setupActionBarWithNavController(navController, appBarConfiguration)

Or change the NoActionBar to DarkActionBar or any others.



来源:https://stackoverflow.com/questions/58861123/why-does-my-app-crash-invoking-setupactionbarwithnavcontrollernavcontroller-a

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!