Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/231.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 片段的onResume()中的getview()_Android_Android Fragments_Nullpointerexception - Fatal编程技术网

Android 片段的onResume()中的getview()

Android 片段的onResume()中的getview(),android,android-fragments,nullpointerexception,Android,Android Fragments,Nullpointerexception,我在片段的onResume()中有以下代码 getView().setFocusableInTouchMode(true); getView().requestFocus(); 由于此代码是在恢复时从内部调用的,因此我没有使用空检查 所以,我不是这样写的: if(getView() != null){ getView().setFocusableInTouchMode(true); getView().requestFocus(); } 我的问题是:在这种情况下跳过空检查可以吗,还是我应该向

我在片段的
onResume()
中有以下代码

getView().setFocusableInTouchMode(true);
getView().requestFocus();
由于此代码是在恢复时从内部调用的,因此我没有使用空检查

所以,我不是这样写的:

if(getView() != null){
getView().setFocusableInTouchMode(true);
getView().requestFocus();
}
我的问题是:在这种情况下跳过空检查可以吗,还是我应该向空检查添加代码

在什么情况下,
getview
()在
onResume
()中将为空?

根据,
getview()
onResume
中不应为
null
。然而,在这种情况下,防守并不会带来真正的伤害。不过,我可能会将视图拉入本地引用,而不是调用
getView()
三次

View view = getView();
if (view != null) {
    view.setFocusableInTouchMode(true);
    view.requestFocus();
}

我相信在
onResume()
中,唯一可能是
null
的情况是在
onCreateView()中返回
null