android - Updating fragment view after AsyncTask finishes returns nullPointerException -
i developing app uses 3 fragments, (which actual pages). 1 fragment used listener, , on 2 others data being downloaded, , set textviews. working fine, until thought cool if fragment view update/refresh after async finished. problem throws me nullpointer exceptions, every time try updating fragment.
btw. think might taking wrong approach type of fragments management, working, , want small cool feature, thought maybe don't need rewrite whole program.
so mine mainactivity.java
public class mainactivity extends fragmentactivity {
//some uninportant initalizations @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); // create adapter return fragment each of 3 // primary sections of app. msectionspageradapter = new sectionspageradapter( getsupportfragmentmanager()); // set viewpager sections adapter. mviewpager = (viewpager) findviewbyid(r.id.pager); mviewpager.setoffscreenpagelimit(3); mviewpager.setadapter(msectionspageradapter); mviewpager.setcurrentitem(1); autogidas agidas = new autogidas(); } public class sectionspageradapter extends fragmentstatepageradapter { public sectionspageradapter(fragmentmanager fm) { super(fm); } @override public fragment getitem(int position) { fragment fragment = new pagrindinis(); switch (position) { case 0: return fragment = new autogidas(); case 1: return fragment = new pagrindinis(); case 2: return fragment = new autoplius(); default: break; } return fragment; } @override public int getcount() { // show 3 total pages. return 3; } @override public charsequence getpagetitle(int position) { locale l = locale.getdefault(); switch (position) { case 0: return getstring(r.string.title_section1).touppercase(l); case 1: return getstring(r.string.title_section2).touppercase(l); case 2: return getstring(r.string.title_section3).touppercase(l); } return null; } }
}
public class autogidas extends fragment {
textview dummytextviewa; mainactivity mactivity; string texttoset; context cont; stringbuffer c; mysqlitehelper db; sharedpreferences prefs; view rootview; arraylist<car> cars; @override public void oncreate(bundle savedinstancestate) { // todo auto-generated method stub super.oncreate(savedinstancestate); } public void setdb(context con) { db = new mysqlitehelper(con); } @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { rootview = inflater.inflate(r.layout.autogidas, container, false); dummytextviewa = (textview) rootview.findviewbyid(r.id.section_label); cont = getactivity(); setdb(cont.getapplicationcontext()); dummytextviewa.settext(""); settextview();//function return new data database mactivity = new mainactivity(); log.i("oncreateview", "oncreateview"); return rootview; } @override public void onsaveinstancestate(bundle outstate) { super.onsaveinstancestate(outstate); log.v("saveinstacestate", "in frag's on save instance state "); outstate.putstring("textview", dummytextviewa.tostring()); } @override public void onviewstaterestored(bundle savedinstancestate) { // todo auto-generated method stub super.onviewstaterestored(savedinstancestate); log.v("restored", "inside of onrestoreinstancestate"); if (savedinstancestate != null) { dummytextviewa.settext(savedinstancestate.getstring("textview")); ; } } public context conts(context con) { return cont = con; } public void sendnotif(context con, string data) { checknetwork.sendnotification(con, data); } public void download() { //function start asynctask } private class downloadfilestask extends asynctask<void, void, string> { @override protected void onpreexecute() { super.onpreexecute(); } private boolean checkifpromoted() { //asynctaskstuff } @override protected string doinbackground(void... params) { //even more asynctask stuff } return texttoset; } @override protected void onpostexecute(string result) { autogidas agidas = new autogidas(); agidas.getfragmentmanager().begintransaction().detach(agidas).commit(); agidas.getfragmentmanager().begintransaction().attach(agidas).commit(); super.onpostexecute(result); } }
}
also fragment used trigger start asynctask
public class pagrindinis extends fragment { /** * fragment argument representing section number * fragment. */ public static final string arg_section_number = "section_number"; public void dummysectionfragment() { } context cont; textview dummytextview; button pradeti; fragment autogidas; @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { view rootview = inflater.inflate(r.layout.pagrindinis, container, false); dummytextview = (textview) rootview.findviewbyid(r.id.section_label); dummytextview.settext("pagrindinis :)"); pradeti = (button) rootview.findviewbyid(r.id.pradeti); pradeti.setonclicklistener(new gidaslistener()); cont = getactivity(); return rootview; } @override public void onsaveinstancestate(bundle outstate) { super.onsaveinstancestate(outstate); log.v("saveinstacestate", "in frag's on save instance state "); outstate.putstring("textview", dummytextview.tostring()); } @override public void onviewstaterestored(bundle savedinstancestate) { // todo auto-generated method stub super.onviewstaterestored(savedinstancestate); log.v("restored", "inside of onrestoreinstancestate"); if(savedinstancestate!=null){ dummytextview.settext(savedinstancestate.getstring("textview"));; } } public class gidaslistener implements onclicklistener { @override public void onclick(view v) { // todo auto-generated method stub log.d("pliuslistener", "button clicked"); autogidas agidas = new autogidas(); autoplius aplius = new autoplius(); agidas.addurls(cont.getapplicationcontext()); agidas.conts(cont.getapplicationcontext()); agidas.setdb(cont.getapplicationcontext()); aplius.addurls(cont.getapplicationcontext()); aplius.conts(cont.getapplicationcontext()); aplius.setdb(cont.getapplicationcontext()); if (checknetwork.isinternetavailable(cont.getapplicationcontext())) { agidas.download(); aplius.download(); log.d("download", "download" ); } else { dummytextview.settext("prisijunkite prie interneto"); } } } }
++logcat:
04-22 19:49:24.810: w/dalvikvm(26354): threadid=1: thread exiting uncaught exception (group=0x40a8ca20) 04-22 19:49:24.830: e/androidruntime(26354): fatal exception: main 04-22 19:49:24.830: e/androidruntime(26354): java.lang.nullpointerexception 04-22 19:49:24.830: e/androidruntime(26354): @ com.example.carseeker.autogidas$downloadfilestask.onpostexecute(autogidas.java:303) 04-22 19:49:24.830: e/androidruntime(26354): @ com.example.carseeker.autogidas$downloadfilestask.onpostexecute(autogidas.java:1) 04-22 19:49:24.830: e/androidruntime(26354): @ android.os.asynctask.finish(asynctask.java:602) 04-22 19:49:24.830: e/androidruntime(26354): @ android.os.asynctask.access$600(asynctask.java:156) 04-22 19:49:24.830: e/androidruntime(26354): @ android.os.asynctask$internalhandler.handlemessage(asynctask.java:615) 04-22 19:49:24.830: e/androidruntime(26354): @ android.os.handler.dispatchmessage(handler.java:99) 04-22 19:49:24.830: e/androidruntime(26354): @ android.os.looper.loop(looper.java:137) 04-22 19:49:24.830: e/androidruntime(26354): @ android.app.activitythread.main(activitythread.java:4424) 04-22 19:49:24.830: e/androidruntime(26354): @ java.lang.reflect.method.invokenative(native method) 04-22 19:49:24.830: e/androidruntime(26354): @ java.lang.reflect.method.invoke(method.java:511) 04-22 19:49:24.830: e/androidruntime(26354): @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:787) 04-22 19:49:24.830: e/androidruntime(26354): @ com.android.internal.os.zygoteinit.main(zygoteinit.java:554) 04-22 19:49:24.830: e/androidruntime(26354): @ dalvik.system.nativestart.main(native method)
i dealing problem last 5 hours, , don't see how deal it.
so maybe me saying , methods should place. think best approach use fragment attach/detach, using send nullpointer exception. if me grateful.
in onpostexecute():
autogidas agidas = new autogidas(); agidas.getfragmentmanager().begintransaction().detach(agidas).commit(); agidas.getfragmentmanager().begintransaction().attach(agidas).commit();
i think you're trying detach previous fragment (created in gidaslistener) name of agidas
, detach new fragment (current agidas
). case? if so, guess possibly use
getfragmentmanager().detach(autogidas.this).commit()
to reference current fragment (one you're trying detach), though don't know detaching current fragment do.
Comments
Post a Comment