我有一个底部导航抽屉的问题,我的意思是我想从底部栏设置一个导航抽屉,所以我尝试使用BottomAppBar并设置我的导航视图,这是我的代码:
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#C4C4C4">
<androidx.coordinatorlayout.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="?attr/actionBarSize">
<FrameLayout
android:id="@+id/scrim"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<com.google.android.material.navigation.NavigationView
android:id="@+id/nav_host_fragment_apoya_test"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_behavior="com.google.android.material.bottomsheet.BottomSheetBehavior"
app:behavior_hideable="true"/>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
<com.google.android.material.bottomappbar.BottomAppBar
android:id="@+id/bottomAppBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:background="@color/white"
app:menu="@menu/bottom_nav_menu_apoya"
app:navigationIcon="@drawable/ic_menu_24"/>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
在我的NavigationView中,我设置了这个参数:
app:layout_behavior="com.google.android.material.bottomsheet.BottomSheetBehavior">
So this is my code:
navigationViewTest = findViewById(R.id.nav_host_fragment_apoya_test);
bottomAppBar = findViewById(R.id.bottomAppBar);
bottomAppBar.setNavigationOnClickListener(this);
bottomSheetBehavior.from(navigationViewTest);
//bottomSheetBehavior.setPeekHeight(ActionBar);
//This line is where I have the problem
bottomSheetBehavior.addBottomSheetCallback(new BottomSheetBehavior.BottomSheetCallback() {
@Override
public void onStateChanged(@NonNull View bottomSheet, int newState) {
if (newState == BottomSheetBehavior.STATE_EXPANDED) {
Toast.makeText(MainActivityApoyaLogin.this, "Expandido", Toast.LENGTH_SHORT).show();
}
if (newState == BottomSheetBehavior.STATE_COLLAPSED) {
Toast.makeText(MainActivityApoyaLogin.this, "Oculto", Toast.LENGTH_SHORT).show();
}
}
@Override
public void onSlide(@NonNull View bottomSheet, float slideOffset) {
// Si tuvieramos un float button aqui lo ocultariamos
}
});
但当我尝试运行我的应用程序时,我得到了这个错误:
2022-09-01 18:33:59.866 11708-11708/com.seccion.seccin15hibrida E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.seccion.seccin15hibrida, PID: 11708
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.seccion.seccin15hibrida/com.seccion.seccin15hibrida.uiCajaSession.InicioCajaA.CajaApoyaBeta.MainActivityApoyaLogin}: java.lang.NullPointerException: Attempt to invoke virtual method 'void com.google.android.material.bottomsheet.BottomSheetBehavior.addBottomSheetCallback(com.google.android.material.bottomsheet.BottomSheetBehavior$BottomSheetCallback)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3449)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3601)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2066)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:223)
at android.app.ActivityThread.main(ActivityThread.java:7656)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void com.google.android.material.bottomsheet.BottomSheetBehavior.addBottomSheetCallback(com.google.android.material.bottomsheet.BottomSheetBehavior$BottomSheetCallback)' on a null object reference
at com.seccion.seccin15hibrida.uiCajaSession.InicioCajaA.CajaApoyaBeta.MainActivityApoyaLogin.onCreate(MainActivityApoyaLogin.java:49)
at android.app.Activity.performCreate(Activity.java:7994)
at android.app.Activity.performCreate(Activity.java:7978)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1309)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3422)
试试这个
更改
bottomSheetBehavior.from(navigationViewTest);
至
bottomSheetBehavior = BottomSheetBehavior.from(navigationViewTest);