【问题标题】:Null pointer exception while building and running bottomnavigation activity构建和运行底部导航活动时出现空指针异常
【发布时间】:2020-09-27 22:32:35
【问题描述】:

我在片段中有一个button,单击该button 会引导我进入一个具有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" />  

重定向到新活动的代码:

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>  

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

原因:java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法“void androidx.appcompat.app.ActionBar.setTitle(java.lang.CharSequence)” 在 androidx.navigation.ui.ActionBarOnDestinationChangedListener.setTitle(ActionBarOnDestinationChangedListener.java:48) 在 androidx.navigation.ui.AbstractAppBarOnDestinationChangedListener.onDestinationChanged(AbstractAppBarOnDestinationChangedListener.java:103)
从哪里调用ActionBar? 在 androidx.navigation.NavController.addOnDestinationChangedListener(NavController.java:233) 在 androidx.navigation.ui.NavigationUI.setupActionBarWithNavController(NavigationUI.java:227) 在 com.dell.mycampus.view.ui._activity.DriveActivity.onCreate(DriveActivity.java:27)

【问题讨论】:

  • 您的活动是否使用基于NoActionBar 的主题?
  • 不...这是我在AndroidManifest&lt;activity android:name=".view.ui._activity.DriveActivity" android:label="@string/title_activity_drive"/&gt;中的代码
  • 您的application 级别的android:theme 是什么?该主题是否使用NoActionBar
  • 它使用android:theme="@style/AppTheme",其父级是Theme.AppCompat.Light.Dialog.Alert
  • 所以为了清楚起见,您的整个应用程序和其中的每个活动的主题都适合AlertDialog?这是你故意的吗?

标签: java android navigation


【解决方案1】:

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

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

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

【讨论】:

  • 好的...我将在我的日历中添加一个注释,以便与添加Theme.AppCompat.Light.Dialog.Alert 作为父样式的开发人员交谈。谢谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-04-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多