我的应用程序中有一个保留的片段,它从webservice下载一些数据并在listview中显示结果。这是我对oncreate和oncreate视图的实现
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//retain fragment for configuration change
setRetainInstance(true);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
if (savedInstanceState == null) {
// i'm here when fragment is created first time
view = inflater.inflate(R.layout.fragment, container, false);
// call a private method for init my widgets
setWidgets(view);
//call here asynctask for download data
} else {
// i'm here after a configuration change, just update views
String myData = MyApp.getStoredData().toString();
// update my widget with previous stored data
}
}
MyApp是我的类,它扩展了Application,并保留了asynctask下载的数据。 现在,如果我在AsyncTask完成时旋转设备,那么就可以了,但是如果我在AsyncTask尚未完成的情况下旋转设备,我在调用MyApp.getStoredData()时会获得一个nullpointer异常.toString()(我在null上调用toString)对象)。
我该如何解决?