相关疑难解决方法(0)

NullPointerException onActivityCreated/onStart/onViewCreated方法中的getView()警告

我知道getView()可能会在onCreateView()方法内部返回null ,但即使我将下面的代码放在里面onActivityCreated(),onStart()或者onViewCreated()方法,它仍然会NullPointerException在Android Studio中显示有关可能的警告(尽管我的程序运行没有任何问题).如何摆脱这种警告?

我正在使用碎片.

码:

datpurchased = (EditText) getView().findViewById(R.id.datepurchased); 
//datpurchased defined as instance variable in the class
Run Code Online (Sandbox Code Playgroud)

警告:

方法调用'getView().findViewById(R.id.datepurchased)'可能会产生'java.lang.NullPointerException'

java android nullpointerexception android-studio getview

10
推荐指数
1
解决办法
5152
查看次数