英文:
Null pointer exception while building and running bottomnavigation activity
问题
我在片段中有一个“按钮”,点击该“按钮”会将我带到一个具有“底部导航”的新活动中。
按钮:
<androidx.appcompat.widget.AppCompatButton
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/driveBtn"
android:layout_below="@id/recyclerview"
android:layout_marginTop="10sp"
android:text="预订出租车"
android:background="@drawable/buttonshape"
android:layout_marginLeft="16sp"
android:layout_marginRight="16sp"
android:textColor="@color/white" />
重定向到新活动的代码:
case R.id.driveBtn:
Intent intent3 = new Intent(getContext(), DriveActivity.class);
startActivity(intent3);
break;
DriveActivity:
public class DriveActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_drive);
BottomNavigationView navView = findViewById(R.id.nav_view);
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);
}
}
activity_drive:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingTop="?attr/actionBarSize"
tools:context=".view.ui._activity.DriveActivity">
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/nav_view"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="0dp"
android:layout_marginEnd="0dp"
android:background="?android:attr/windowBackground"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:menu="@menu/bottom_nav_menu"
app:labelVisibilityMode="labeled" />
<fragment
android:id="@+id/nav_host_fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:defaultNavHost="true"
app:layout_constraintBottom_toTopOf="@id/nav_view"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:navGraph="@navigation/mobile_navigation" />
</androidx.constraintlayout.widget.ConstraintLayout>
当我尝试运行该应用时,我收到以下错误消息:
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:103)
From where is that 'ActionBar' being called?
at androidx.navigation.NavController.addOnDestinationChangedListener(NavController.java:233)
at androidx.navigation.ui.NavigationUI.setupActionBarWithNavController(NavigationUI.java:227)
at com.dell.mycampus.view.ui._activity.DriveActivity.onCreate(DriveActivity.java:27)
英文:
I've a button
in a fragment, on click of that button
leads me to a new activity which has bottomnavigation
.
Button:
<androidx.appcompat.widget.AppCompatButton
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/driveBtn"
android:layout_below="@id/recyclerview"
android:layout_marginTop="10sp"
android:text="Book a Cab"
android:background="@drawable/buttonshape"
android:layout_marginLeft="16sp"
android:layout_marginRight="16sp"
android:textColor="@color/white" />
Code to redirect to new activity:
case R.id.driveBtn:
Intent intent3 = new Intent(getContext(), DriveActivity.class);
startActivity(intent3);
break;
DriveActivity:
public class DriveActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_drive);
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);
}
}
activity_drive:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingTop="?attr/actionBarSize"
tools:context=".view.ui._activity.DriveActivity">
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/nav_view"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="0dp"
android:layout_marginEnd="0dp"
android:background="?android:attr/windowBackground"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:menu="@menu/bottom_nav_menu"
app:labelVisibilityMode="labeled" />
<fragment
android:id="@+id/nav_host_fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:defaultNavHost="true"
app:layout_constraintBottom_toTopOf="@id/nav_view"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:navGraph="@navigation/mobile_navigation" />
</androidx.constraintlayout.widget.ConstraintLayout>
When I try running that app, I get this error:
> 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:103)
From where is that ActionBar
being called?
at androidx.navigation.NavController.addOnDestinationChangedListener(NavController.java:233)
at androidx.navigation.ui.NavigationUI.setupActionBarWithNavController(NavigationUI.java:227)
at com.dell.mycampus.view.ui._activity.DriveActivity.onCreate(DriveActivity.java:27)
答案1
得分: 1
Theme.AppCompat.Light.Dialog.Alert
是适用于Dialog
的主题。具体来说,适用于AlertDialog
。对话框没有操作栏,因此预期使用该主题的活动将没有操作栏,导致getSupportActionBar()
始终返回null。
当你调用setupActionBarWithNavController()
时,你是想要在你的NavController中设置操作栏。由于没有操作栏,预期每次调用setTitle
、setDisplayHomeAsUp()
以及其他ActionBar
API都会失败并引发NullPointerException
。
如果你想要一个默认包含操作栏的主题,你应该为你的活动使用Theme.AppCompat.Light
主题。
英文:
Theme.AppCompat.Light.Dialog.Alert
is a theme appropriate for a Dialog
. Specifically, an AlertDialog
. Dialogs do not have an action bar, so it is expected that an Activity using that theme will not have an action bar, resulting in getSupportActionBar()
always returning null.
When you say setupActionBarWithNavController()
you are saying you want to set up your action bar with your NavController. Since have no action bar, it is expected that every call to setTitle
, setDisplayHomeAsUp()
and other ActionBar
API fail with a NullPointerException
.
If you want a theme that includes an action bar by default, you should use a Theme.AppCompat.Light
theme for your activity.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论