Kaidul
Kaidul

Reputation: 15875

Application crushes and throw NullPointerException while Orientation is changing

I am using Fragment and put AsyncTask inside it to do some network oriented task. Everything works fine. When I change the orientation from protrait to landscape, it also wors fine. But when I changed the orientation again to portrait, it crashes. This is my trying code.

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);
        }
    }

}

And this is my error log:

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)

Upvotes: 1

Views: 752

Answers (1)

Goca
Goca

Reputation: 1873

When orientation of the phone changes, your activity is destroyed and created again.

You can use this in AndroidManifest.xml.

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

Edited:

As complementary for this answer:

The documentations says:"A fragment must always be embedded in an activity and the fragment's lifecycle is directly affected by the host activity's lifecycle. For example, when the activity is paused, so are all fragments in it, and when the activity is destroyed, so are all fragments."

Here is the link http://developer.android.com/guide/components/fragments.html

You can't recreate your activity only for some fragments, you should rethink your approach.

Upvotes: 4

Related Questions