问题
I added a Bottom Navigation Activity to my project but when i start the relevant activity it gives out a Null Pointer Exception.
It says that the exception is fired at line number 28
at lk.apiit.eea.mobile.Activities.CompanyProfile.onCreate(CompanyProfile.java:28)
Following is the Exception and the Code PLease help me fix this.. Thank you
java.lang.RuntimeException: Unable to start activity ComponentInfo{lk.apiit.eea.mobile/lk.apiit.eea.mobile.Activities.CompanyProfile}: 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:2720)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2781)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1508)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:241)
at android.app.ActivityThread.main(ActivityThread.java:6274)
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 lk.apiit.eea.mobile.Activities.CompanyProfile.onCreate(CompanyProfile.java:28)
at android.app.Activity.performCreate(Activity.java:6720)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2673)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2781)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1508)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:241)
at android.app.ActivityThread.main(ActivityThread.java:6274)
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)
An this is the code in onCreateMethod
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_company_profile);
BottomNavigationView navView = findViewById(R.id.nav_view);
// Passing each menu ID as a set of Ids because each
// menu should be considered as top level destinations.
AppBarConfiguration appBarConfiguration = new AppBarConfiguration.Builder(
R.id.navigation_home, R.id.navigation_dashboard, R.id.navigation_notifications)
.build();
NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
NavigationUI.setupActionBarWithNavController(this, navController, appBarConfiguration);
NavigationUI.setupWithNavController(navView, navController);
}
回答1:
If you aren't calling setSupportActionBar()
and you are using a NoActionBar
theme, then you don't have an ActionBar. setupActionBarWithNavController()
assumes you have an ActionBar, hence the NullPointerException.
You should switch to a theme that provides an ActionBar or call setSupportActionBar()
with a Toolbar
in your layout before calling setupActionBarWithNavController()
.
回答2:
Navigation graph is searching for the default app bar and in case if you don't have a default action bar in your code then please remove the line.
NavigationUI.setupActionBarWithNavController(this, navController, appBarConfiguration);
来源:https://stackoverflow.com/questions/60214377/android-bottom-navigation-activity-gives-a-null-pointer-exception-in-oncreate