NullPointerException with AppCompat BottomSheets

前端 未结 5 1546
LinearLayout bottomSheetViewgroup = (LinearLayout) findViewById(R.id.bottomSheet);

bottomSheetBehavior = BottomSheetBehavior.from(bottomSheetViewgroup);

bottomShee         


        
5条回答
  •  Happy的楠姐
    2020-12-20 12:37

    The issue with your code is you are trying to call the setState method directly inside onCreate. This is will throw a nullPointer because the WeakReference is not initialized yet. It will get initialized when the Coordinator layout is about to lay its child view.

    onLayoutChild(CoordinatorLayout parent, V child, int layoutDirection)

    Called when the parent CoordinatorLayout is about the lay out the given child view.

    So the best approach is set the peek height to 0 and show/hide inside the onItemClick listener.

    I have answered this question here: https://stackoverflow.com/a/36236743/1314796

提交回复
热议问题