Android Fragment lifecycle makes app crash -
when fragment
displaying info custom session
object created, need load ui properties selected session
.
this works fine when fragment
created first time wrote code set in onstart
, because layout isn't inflated until oncreateview
, called before onstart
. (http://developer.android.com/guide/components/fragments.html)
the problem : whenever phone rotated, app crashes, apparently because onstart
gets called,
i tried using onactivitycreated
, onresume
instead neither worked, , 3 functions called automatically after oncreateview
what can without disabling rotation ?
there diagram of fragment lifecycle here: http://developer.android.com/guide/components/fragments.html
05-12 01:03:12.968 21599-21599/com.example.kn.ib w/dalvikvm﹕ threadid=1: thread exiting uncaught exception (group=0x418f3da0) 05-12 01:03:12.968 21599-21599/com.example.kn.ib e/androidruntime﹕ fatal exception: main process: com.example.kn.ib, pid: 21599 java.lang.runtimeexception: unable start activity componentinfo{com.example.kn.ib/com.example.kn.ib.ibcentralactivity}: java.lang.nullpointerexception @ android.app.activitythread.performlaunchactivity(activitythread.java:2305) @ android.app.activitythread.handlelaunchactivity(activitythread.java:2363) @ android.app.activitythread.handlerelaunchactivity(activitythread.java:3924) @ android.app.activitythread.access$1000(activitythread.java:161) @ android.app.activitythread$h.handlemessage(activitythread.java:1271) @ android.os.handler.dispatchmessage(handler.java:102) @ android.os.looper.loop(looper.java:157) @ android.app.activitythread.main(activitythread.java:5356) @ java.lang.reflect.method.invokenative(native method) @ java.lang.reflect.method.invoke(method.java:515) @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:1265) @ com.android.internal.os.zygoteinit.main(zygoteinit.java:1081) @ dalvik.system.nativestart.main(native method) caused by: java.lang.nullpointerexception @ com.example.karimnseir.instabeat.ibsessioninfofragment.onactivitycreated(ibsessioninfofragment.java:117) @ android.app.fragment.performactivitycreated(fragment.java:1708) @ android.app.fragmentmanagerimpl.movetostate(fragmentmanager.java:908) @ android.app.fragmentmanagerimpl.movetostate(fragmentmanager.java:1062) @ android.app.fragmentmanagerimpl.movetostate(fragmentmanager.java:1044) @ android.app.fragmentmanagerimpl.dispatchactivitycreated(fragmentmanager.java:1853) @ android.app.activity.performcreate(activity.java:5429) @ android.app.instrumentation.callactivityoncreate(instrumentation.java:1105) @ android.app.activitythread.performlaunchactivity(activitythread.java:2269) at android.app.activitythread.handlelaunchactivity(activitythread.java:2363) at android.app.activitythread.handlerelaunchactivity(activitythread.java:3924) at android.app.activitythread.access$1000(activitythread.java:161) at android.app.activitythread$h.handlemessage(activitythread.java:1271) at android.os.handler.dispatchmessage(handler.java:102) at android.os.looper.loop(looper.java:157) at android.app.activitythread.main(activitythread.java:5356) at java.lang.reflect.method.invokenative(native method) at java.lang.reflect.method.invoke(method.java:515) at com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:1265) at com.android.internal.os.zygoteinit.main(zygoteinit.java:1081) at dalvik.system.nativestart.main(native method)
fragment onstart (line 117 "id: "+ session._id + "\n" +
)
@override public void onstart() { super.onstart(); sessionid = activitycallback.getselectedsessionid(); db = activitycallback.getsessiondatabase(); instabeattimeservice = new instabeatdateandtime(); session = db.getsession(sessionid); textview textviewfortesting = (textview) getview().findviewbyid(r.id.test); string testtext = "\n" + "id: "+ session._id + "\n" + "lap count: "+ session._lapcount + "\n" + "date: "+ session._date + "\n" + "calories: "+ session._calories + "\n" + "isopen: "+ session._isopen + "\n" + "lastmod: "+ session._lastmod; textviewfortesting.settext(testtext); button savebutton = (button) getview().findviewbyid(r.id.sessioninfo_savebutton); savebutton.setonclicklistener(this); isopentextfield = (edittext) getview().findviewbyid(r.id.isopen); poolsizetextfield = (edittext) getview().findviewbyid(r.id.poolsize); commenttextfield = (edittext) getview().findviewbyid(r.id.comment); metrictextfield = (edittext) getview().findviewbyid(r.id.metric); date sessiondate = instabeattimeservice.getdatefromlong(session._date); int sessionday = instabeattimeservice.getdayfromdate(sessiondate); int sessionmonth = instabeattimeservice.getmonthfromdate(sessiondate); int sessionyear = instabeattimeservice.getyearfromdate(sessiondate); int sessionhour = instabeattimeservice.gethourfromdate(sessiondate); int sessionminute = instabeattimeservice.getminutefromdate(sessiondate); datefield = (edittext) getview().findviewbyid(r.id.sessiondatefield); dateformatter = new simpledateformat("mm/dd/yyyy", locale.us); datefield.setinputtype(inputtype.type_null); datefield.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { datepickerdialog.show(); } }); datefield.settext(sessionmonth + "/" + sessionday + "/" + sessionyear); datepickerdialog = new datepickerdialog(getactivity(), new datepickerdialog.ondatesetlistener() { public void ondateset(datepicker view, int year, int monthofyear, int dayofmonth) { calendar newdate = calendar.getinstance(); newdate.set(year, monthofyear, dayofmonth); datefield.settext(dateformatter.format(newdate.gettime())); } },sessionyear, sessionmonth, sessionday); timefield = (edittext) getview().findviewbyid(r.id.sessiontimefield); //todo: set user preferences time format hh or kk timeformatter = new simpledateformat("hh:mm", locale.us); timefield.setinputtype(inputtype.type_null); timefield.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { timepickerdialog.show(); } }); timefield.settext(sessionhour + ":" + sessionminute); timepickerdialog = new timepickerdialog(getactivity(), new timepickerdialog.ontimesetlistener() { public void ontimeset(timepicker view, int hourofday, int minute) { calendar newtime = calendar.getinstance(); int hr = newtime.get(calendar.hour_of_day); int mn = newtime.get(calendar.minute); /*todo: set user preferences time format hh or kk */ datefield.settext(hr+":"+mn); } },sessionhour, sessionminute, true/*todo: set user preferences time format hh or kk */); isopentextfield.settext(session._isopen+""); metrictextfield.settext(session._metric); commenttextfield.settext(session._comment); poolsizetextfield.settext("" + session._poolsize); }
activity , fragment lifecycle not trivial on android. please remember activity , fragment destroyed , recreated on device rotation so,if need method not called again can use oncreate method of fragment , put setretaininstancestate(true) inside same method. in way fragment retained , not destroyed on device orientation changes. must define fragment in xml layout , must assign id, if layout added programmaticcally or not have ui can use fragmentmanager , tag add , retrieve fragment.
you can check on android handling runtime changes guide
Comments
Post a Comment