我試圖從Android支持設計庫實現BottomSheetBehavior
。我初始化BottomSheetBehavior
這樣的:Android BottomSheetBehavior setState()NullPointerException
private void initBottomSheet() {
new AsyncTask<Void, Void, Void>() {
View bottomSheetFrame = rootView.findViewById(R.id.bottom_sheet);
}
@Override
protected Void doInBackground(Void... params) {
bottomSheetBehavior = BottomSheetBehavior.from(bottomSheetFrame);
bottomSheetBehavior.setBottomSheetCallback(new BottomSheetBehavior.BottomSheetCallback() {
private boolean isOnTop = false;
@Override
public void onStateChanged(@NonNull View bottomSheet, int newState) {
switch (newState) {
case BottomSheetBehavior.STATE_DRAGGING: {
...
}
case BottomSheetBehavior.STATE_SETTLING: {
...
}
case BottomSheetBehavior.STATE_EXPANDED: {
...
}
case BottomSheetBehavior.STATE_COLLAPSED: {
...
}
case BottomSheetBehavior.STATE_HIDDEN: {
...
}
default: {
break;
}
}
}
@Override
public void onSlide(@NonNull View bottomSheet, float slideOffset) {
...
});
bottomSheetBehavior.setPeekHeight((int) Utils.convertDpToPixel(100f, activityContext));
bottomSheetBehavior.setState(BottomSheetBehavior.STATE_HIDDEN); // NPE here
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
}
}.execute();
}
這是非常奇怪的,因爲我可以Button
點擊或其他一些行動改變狀態。請幫幫我。
刪除此行查看bottomSheetFrame = rootView.findViewById (R.id.bottom_sheet);異步任務的異常,並在onCreate方法中查找視圖。另外,如果你正在更新視圖,你應該在onPostExecute方法中做。 – AndroidRuntimeException
@AgustinSivoplás,非常感謝。我的事件沒有注意到從'doInBackground()'調用方法。 –