【发布时间】: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、<activity android:name=".view.ui._activity.DriveActivity" android:label="@string/title_activity_drive"/>中的代码 -
您的
application级别的android:theme是什么?该主题是否使用NoActionBar? -
它使用
android:theme="@style/AppTheme",其父级是Theme.AppCompat.Light.Dialog.Alert。 -
所以为了清楚起见,您的整个应用程序和其中的每个活动的主题都适合
AlertDialog?这是你故意的吗?
标签: java android navigation