android - Application crushes and throw NullPointerException while Orientation is changing -
i using fragment
, put asynctask
inside network oriented task. works fine. when change orientation protrait landscape, wors fine. when changed orientation again portrait
, crashes. 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 , parsing related code layout.addview(gview); setcontentshown(true); } } }
and error log:
08-16 00:07:18.025: e/androidruntime(542): java.lang.nullpointerexception 08-16 00:07:18.025: e/androidruntime(542): @ me.kaidul.uhunt.latestsubmissions$getsubmissionslisttask.onpostexecute(latestsubmissions.java:84) 08-16 00:07:18.025: e/androidruntime(542): @ me.kaidul.uhunt.latestsubmissions$getsubmissionslisttask.onpostexecute(latestsubmissions.java:1) 08-16 00:07:18.025: e/androidruntime(542): @ android.os.asynctask.finish(asynctask.java:417) 08-16 00:07:18.025: e/androidruntime(542): @ android.os.asynctask.access$300(asynctask.java:127) 08-16 00:07:18.025: e/androidruntime(542): @ android.os.asynctask$internalhandler.handlemessage(asynctask.java:429) 08-16 00:07:18.025: e/androidruntime(542): @ android.os.handler.dispatchmessage(handler.java:99) 08-16 00:07:18.025: e/androidruntime(542): @ android.os.looper.loop(looper.java:123) 08-16 00:07:18.025: e/androidruntime(542): @ android.app.activitythread.main(activitythread.java:4627) 08-16 00:07:18.025: e/androidruntime(542): @ java.lang.reflect.method.invokenative(native method) 08-16 00:07:18.025: e/androidruntime(542): @ java.lang.reflect.method.invoke(method.java:521) 08-16 00:07:18.025: e/androidruntime(542): @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:868) 08-16 00:07:18.025: e/androidruntime(542): @ com.android.internal.os.zygoteinit.main(zygoteinit.java:626) 08-16 00:07:18.025: e/androidruntime(542): @ dalvik.system.nativestart.main(native method)
when orientation of phone changes, activity destroyed , created again.
you can use in androidmanifest.xml.
activity activity... android:configchanges="orientation|keyboardhidden|screensize"> activity
edited:
as complementary answer:
the documentations says:"a fragment must embedded in activity , fragment's lifecycle directly affected host activity's lifecycle. example, when activity paused, fragments in it, , when activity destroyed, fragments."
here link http://developer.android.com/guide/components/fragments.html
you can't recreate activity fragments, should rethink approach.
Comments
Post a Comment