构建和 ​​运行 底部导航 activity 时出现空指针异常

Null pointer exception while building and running bottomnavigation activity

我在一个片段中有一个 button,点击那个 button 会引导我进入一个新的 activity,其中有 bottomnavigation按钮:

<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" />  

重定向到新的代码 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>  

当我尝试 运行 该应用程序时,出现此错误:

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)

Theme.AppCompat.Light.Dialog.Alert 是适合 Dialog 的主题。具体来说,一个 AlertDialog。对话框没有操作栏,因此预计使用该主题的 Activity 不会有操作栏,导致 getSupportActionBar() 总是返回 null。

当您说 setupActionBarWithNavController() 时,您是在说您想使用 NavController 设置操作栏。由于没有操作栏,预计每次调用 setTitlesetDisplayHomeAsUp() 和其他 ActionBar API 都会失败并显示 NullPointerException.

如果您想要一个默认包含操作栏的主题,您应该为您的 activity 使用 Theme.AppCompat.Light 主题。