我正在使用Fragment
并放入AsyncTask
其中来执行一些面向网络的任务。一切正常。当我将方向从纵向更改为横向时,它也很糟糕。但是当我再次将方向更改为 时portrait
,它崩溃了。这是我的尝试代码。
public class ProblemStatistics extends SherlockProgressFragment {
View mContentView;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
mContentView = inflater.inflate(R.layout.submission_statistics,
container, false);
return super.onCreateView(inflater, container, savedInstanceState);
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
setContentShown(false);
setContentView(mContentView);
Bundle bundle = this.getArguments();
int problemNo = 100;
if (bundle != null) {
problemNo = bundle.getInt("problem_no", problemNo);
}
new GetProblemStatisticTask().execute(CommonUtils.SPECIFIC_PROBLEM_URL + problemNo);
}
@Override
public void onStop() {
((MainActivity) getSherlockActivity()).setProblemStatistics(false);
super.onStop();
}
protected class GetProblemStatisticTask extends
AsyncTask<String, Void, InputStreamReader> {
@Override
protected InputStreamReader doInBackground(String... params) {
return new JSONDownloader().getJSONStringFromUrl(params[0]);
}
@Override
protected void onPostExecute(InputStreamReader isr) {
super.onPostExecute(isr);
// other stuff and parsing related code
layout.addView(gView);
setContentShown(true);
}
}
}
这是我的错误日志:
08-16 00:07:18.025: E/AndroidRuntime(542): java.lang.NullPointerException
08-16 00:07:18.025: E/AndroidRuntime(542): at me.kaidul.uhunt.LatestSubmissions$GetSubmissionsListTask.onPostExecute(LatestSubmissions.java:84)
08-16 00:07:18.025: E/AndroidRuntime(542): at me.kaidul.uhunt.LatestSubmissions$GetSubmissionsListTask.onPostExecute(LatestSubmissions.java:1)
08-16 00:07:18.025: E/AndroidRuntime(542): at android.os.AsyncTask.finish(AsyncTask.java:417)
08-16 00:07:18.025: E/AndroidRuntime(542): at android.os.AsyncTask.access$300(AsyncTask.java:127)
08-16 00:07:18.025: E/AndroidRuntime(542): at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:429)
08-16 00:07:18.025: E/AndroidRuntime(542): at android.os.Handler.dispatchMessage(Handler.java:99)
08-16 00:07:18.025: E/AndroidRuntime(542): at android.os.Looper.loop(Looper.java:123)
08-16 00:07:18.025: E/AndroidRuntime(542): at android.app.ActivityThread.main(ActivityThread.java:4627)
08-16 00:07:18.025: E/AndroidRuntime(542): at java.lang.reflect.Method.invokeNative(Native Method)
08-16 00:07:18.025: E/AndroidRuntime(542): at java.lang.reflect.Method.invoke(Method.java:521)
08-16 00:07:18.025: E/AndroidRuntime(542): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
08-16 00:07:18.025: E/AndroidRuntime(542): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
08-16 00:07:18.025: E/AndroidRuntime(542): at dalvik.system.NativeStart.main(Native Method)