gpt4 book ai didi

Android 底部导航 Activity 在 onCreate 中给出空指针异常

转载 作者:行者123 更新时间:2023-12-05 00:09:55 44 4
gpt4 key购买 nike

我在我的项目中添加了一个底部导航 Activity ,但是当我开始相关 Activity 时,它给出了一个空指针异常。

它说异常在第 28 行被触发

at lk.apiit.eea.mobile.Activities.CompanyProfile.onCreate(CompanyProfile.java:28)

以下是异常和代码请帮我解决这个问题。谢谢

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) 

这是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);
}

最佳答案

如果您没有调用 setSupportActionBar() 并且您使用的是 NoActionBar 主题,那么您就没有 ActionBar。 setupActionBarWithNavController() 假设您有一个 ActionBar,因此会出现 NullPointerException。

在调用 setupActionBarWithNavController() 之前,您应该切换到提供 ActionBar 的主题或使用布局中的 Toolbar 调用 setSupportActionBar() .

关于Android 底部导航 Activity 在 onCreate 中给出空指针异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60214377/

44 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com