android - Passing data from asynctask to activity view -


there many questions related problem none of them solved problem. tried of them.

i'm trying change values in layout after getting data network source, i'm getting nullpointerexception everytime.

error i'm getting

03-25 18:05:43.071: e/androidruntime(4832): fatal exception: main     03-25 18:05:43.071: e/androidruntime(4832): java.lang.nullpointerexception     03-25 18:05:43.071: e/androidruntime(4832):     @ com.wishberg.app.communitypageactivity$sendrequest.onpostexecute(communitypageactivity.java:158)     03-25 18:05:43.071: e/androidruntime(4832):     @ com.wishberg.app.communitypageactivity$sendrequest.onpostexecute(communitypageactivity.java:1)     03-25 18:05:43.071: e/androidruntime(4832):     @ android.os.asynctask.finish(asynctask.java:631)     03-25 18:05:43.071: e/androidruntime(4832):     @ android.os.asynctask.access$600(asynctask.java:177)     03-25 18:05:43.071: e/androidruntime(4832):     @ android.os.asynctask$internalhandler.handlemessage(asynctask.java:644)     03-25 18:05:43.071: e/androidruntime(4832):     @ android.os.handler.dispatchmessage(handler.java:99)     03-25 18:05:43.071: e/androidruntime(4832):     @ android.os.looper.loop(looper.java:137)     03-25 18:05:43.071: e/androidruntime(4832):     @ android.app.activitythread.main(activitythread.java:5103)     03-25 18:05:43.071: e/androidruntime(4832):     @ java.lang.reflect.method.invokenative(native method)     03-25 18:05:43.071: e/androidruntime(4832):     @ java.lang.reflect.method.invoke(method.java:525)     03-25 18:05:43.071: e/androidruntime(4832):     @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:737)     03-25 18:05:43.071: e/androidruntime(4832):     @ com.android.internal.os.zygoteinit.main(zygoteinit.java:553)     03-25 18:05:43.071: e/androidruntime(4832):     @ dalvik.system.nativestart.main(native method) 

here activity class

public class communitypageactivity extends activity {      public string wishid = null;     private context context = null;     public string tag = "communitypage";      private string cachedname = "community";     private static final int url_loader = 0;     private menu optionsmenu;     private progressdialog progress;      @override     protected void oncreate(bundle savedinstancestate) {         // todo auto-generated method stub         super.oncreate(savedinstancestate);          intent = getintent();         wishid = i.getstringextra("id");         context = getapplicationcontext();          actionbar actionbar = getactionbar();         actionbar.setdisplayshowtitleenabled(false);          common.setrefreshactionbuttonstate(true, optionsmenu);              progress = new progressdialog(this);     progress.setmessage("loading...");     progress.setindeterminate(true);     progress.show();          setcontentview(r.layout.community);     }       private class sendrequest extends asynctask<string, integer, string> {          // http://www.mysamplecode.com/2012/11/android-progress-bar-example.html          viewholder holder = null;         protected string doinbackground(string... requesturl) {              string data = "";             httpurlconnection httpurlconnection = null;              try {                  data = common.readstream(in);              } catch (malformedurlexception exception) {                 log.e(tag, "malformedurlexception");             } catch (ioexception exception) {                 log.e(tag, "ioexception");             } {                 if (null != httpurlconnection)                     httpurlconnection.disconnect();             }             return data;          }          protected void onpostexecute(string jsonstring) {              progress.dismiss();             system.out.println(jsonstring);             jsonarray jsonarray = common.getarrayfromjsonstring(jsonstring, 0);               inflater inflater; //          view v = inflater.inflate(r.layout.community);             holder.txtwishname = (textview) communitypageactivity.this.findviewbyid(r.id.tv_communitywishname);             system.out.println(holder);             system.out.println(jsonarray);         }           /* private view holder class */         private class viewholder {             imageview imageview;             textview txtwishname;             imagebutton btn_touchwood;             imagebutton btn_addwish;             textview tvgesture;             textview tvnotes;         }      }  } 

first of all, not initialize widgets inside asynctask. not use inflater in there. should initialize in oncreate(..) , alike.

then can perform expensive operations (http connection , on) in asynctask. in onpostexecute should update ui widgets, are initialized in activity/fragment. @ least that's how asynctask meant operate.

and mentioned, holder variable not initialized, that's why exception in first place.

upd: moreover, parsing json data in onpostexecute() bad idea, negate effect expect usage of asynctask - method run on ui-thread , freeze it. move stuff doinbackground().


Comments

Popular posts from this blog

java - WrongTypeOfReturnValue exception thrown when unit testing using mockito -

php - Magento - Deleted Base url key -

android - How to disable Button if EditText is empty ? -