Java Android BottomSheetBehavior setState()NullPointerException

Java Android BottomSheetBehavior setState()NullPointerException,java,android,nullpointerexception,android-support-library,android-design-library,Java,Android,Nullpointerexception,Android Support Library,Android Design Library,我正试图从Android支持设计库中实现BottomSheetBehavior。我初始化BottomSheetBehavior如下: private void initBottomSheet() { new AsyncTask<Void, Void, Void>() { View bottomSheetFrame = rootView.findViewById(R.id.bottom_sheet); }

我正试图从Android支持设计库中实现
BottomSheetBehavior
。我初始化
BottomSheetBehavior
如下:

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();
    }
private void initBottomSheet(){
新建异步任务(){
视图bottomSheetFrame=rootView.findViewById(R.id.bottom_-sheet);
}
@凌驾
受保护的Void doInBackground(Void…参数){
bottomSheetBehavior=bottomSheetBehavior.from(bottomSheetFrame);
setBottomSheetCallback(新的bottomSheetBehavior.BottomSheetCallback(){
私有布尔值isOnTop=false;
@凌驾
公共void onStateChanged(@NonNull View bottomSheet,int newState){
交换机(新闻状态){
case BottomSheetBehavior.STATE_拖动:{
...
}
case.STATE_结算:{
...
}
case BottomSheetBehavior.STATE_已展开:{
...
}
case BottomSheetBehavior.STATE_已崩溃:{
...
}
case BottomSheetBehavior.STATE_隐藏:{
...
}
默认值:{
打破
}
}
}
@凌驾
幻灯片上的公共空白(@NonNull视图底页,浮动幻灯片偏移){
...
});
bottomSheetBehavior.setPeekHeight((int)Utils.convertDpToPixel(100f,activityContext));
bottomSheetBehavior.setState(bottomSheetBehavior.STATE_HIDDEN);//此处为NPE
返回null;
}
@凌驾
受保护的void onPostExecute(void避免){
super.onPostExecute(避免);
}
}.execute();
}

这很奇怪,因为我可以通过点击按钮或其他动作来改变状态。请帮帮我。

问题

NPE之所以发生,是因为在布局视图-
BottomSheetBehavior
之前调用了
BottomSheetBehavior.setState(…)
。此时,视图上有
null
引用,无法将您的状态应用于该视图

解决方案

我通过使用
View.post(Runnable)
方法解决了这个问题:

View sheetView = ... ;    
BottomSheetBehavior behavior = BottomSheetBehavior.from(sheetView);
int initState = BottomSheetBehavior.STATE_EXPANDED;

sheetView.post(new Runnable() {
    @Override
    public void run() {
        behavior.setState(initState);
    }
});

在我的情况下,这有助于停止NPE-s:)

如果您在
片段中使用它,请确保在
onCreateView()方法中调用它。如果您在
活动中使用它
,请在
onCreate()
方法中调用它

在这些点上,必须已经创建视图,除非以编程方式创建它们

由于您是从
AsyncTask
调用与UI相关的方法,因此您需要使用
runOnUiThread
将其包围起来,如下所述


基本上,您需要从应用程序的主线程(即UI线程)调用与视图相关的任何内容。

删除此行视图bottomSheetFrame=rootView.findViewById(R.id.bottom\u sheet);退出异步任务,并使其在onCreate方法中查找视图。另外,如果您正在更新一个视图,您应该在onPostExecute方法中执行。@AgustinSivoplás,非常感谢。我没有注意到方法是从
doInBackground()
调用的!谢谢你的回复。很好的解决方案,但就我而言,我只需要将代码从
AsyncTask
中删除即可。很好的解决方案,您让我从一个下午的挫折中解脱出来:)