当Orientation改变时,应用程序会破坏并抛出NullPointerException

时间:2013-08-15 17:28:09

标签: android android-fragments screen-orientation

我正在使用Fragment并在其中放置AsyncTask来执行面向网络的任务。一切正常。当我将方向从protrait改为landscape时,它也很好。但当我再次将方向更改为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)

1 个答案:

答案 0 :(得分:4)

当手机的方向发生变化时,您的活动将被销毁并再次创建。

您可以在AndroidManifest.xml中使用它。

activity
   your activity...
   android:configChanges="orientation|keyboardHidden|screenSize">
 activity

<强>编辑:

作为这个答案的补充:

文档说:&#34;片段必须始终嵌入到活动中,片段的生命周期直接受主机活动生命周期的影响。例如,当活动暂停时,其中的所有片段也都被暂停,当活动被破坏时,所有片段也都被破坏。&#34;

以下是链接http://developer.android.com/guide/components/fragments.html

您不能仅为某些片段重新创建活动,您应该重新考虑您的方法。