问题
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.
Add fragment dependencies on app level build.gradle. implementation 'androidx.fragment:fragment:1.2.4' implementation 'androidx.fragment:fragment-ktx:1.2.4'
Clean build (Build -> Clean Project)
Invalidate Caches and Restart (if developer PC OS is Windows)
Reboot PC (I do not know why. but if I don't reboot PC, the problem occurs again. )
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