bottomsheetbehavior nullpointerexception

uklbhaso  于 2021-06-30  发布在  Java
关注(0)|答案(0)|浏览(208)

下午好!请帮我弄清楚为什么会这样。在另一项活动中,一切正常。。。
原因:java.lang.nullpointerexception:尝试在com.google.android.material.bottomsheet.bottomsheetbehavior.from(bottomsheetbehavior)的空对象引用上调用虚拟方法“android.view.viewgroup$layoutparams android.view.view.getlayoutparams()”。java:1632)在com.example.notes.activities.mainactivity.initbottommenu(mainactivity。java:331)在com.example.notes.activities.mainactivity.oncreate(mainactivity。java:120)在android.app.activity.performcreate(activity。java:7802)在android.app.activity.performcreate(activity。java:7791)在android.app.instrumentation.callactivityoncreate(instrumentation。java:1299)
活动

public class MainActivity extends AppCompatActivity {

    private NotesAdapter notesAdapter;
    private AlertDialog dialogURL;
    private ArrayList<Note> notesFromDB;
    private RecyclerView recyclerViewNotes;
    private Menu menu;
    private String selectedColor;
    private BottomAppBar bottomAppBar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ...

        bottomAppBar = findViewById(R.id.bottom_appbar);
        setSupportActionBar(bottomAppBar);

        ...

        initBottomMenu();
    }

    private void initBottomMenu() {
        final LinearLayout linearLayoutBottomMenu = findViewById(R.id.ll_bottom_menu);
        final BottomSheetBehavior<LinearLayout> bottomSheetBehavior = BottomSheetBehavior.from(linearLayoutBottomMenu);
        bottomAppBar.setNavigationOnClickListener(v -> {
            if (bottomSheetBehavior.getState() != BottomSheetBehavior.STATE_EXPANDED) {
                bottomSheetBehavior.setState(BottomSheetBehavior.STATE_EXPANDED);
            } else {
                bottomSheetBehavior.setState(BottomSheetBehavior.STATE_COLLAPSED);
            }
        });
    }
}

布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/ll_bottom_menu"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/background_palette"
    android:elevation="@dimen/_4sdp"
    android:orientation="vertical"
    android:paddingBottom="@dimen/_4sdp"
    app:behavior_peekHeight="0dp"
    app:layout_behavior="@string/bottom_sheet_behavior">

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题