1

我正在使用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)
4

1 回答 1

4

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

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

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

编辑:

作为这个答案的补充:

文档说:“一个片段必须始终嵌入在一个活动中,片段的生命周期直接受到宿主活动生命周期的影响。例如,当活动暂停时,其中的所有片段也是如此,当活动被销毁时,所有的碎片也是如此。”

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

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

于 2013-08-15T17:36:28.513 回答