android - getActivity() gives context equal to null -


i have developed , launched android application. works fine on android phone, crashes on several devices.

*bugsense gives me following error:* 0   java.lang.nullpointerexception  1   @ android.widget.arrayadapter.init(arrayadapter.java:310)  2   @ android.widget.arrayadapter.(arrayadapter.java:128)  3   **at com.challenger.app.challengeadapter.(challengeadapter.java:27)**  4   @ com.challenger.app.allchallenges.fitchallenges(allchallenges.java:142)  5   @ com.challenger.app.allchallenges$1.onsuccess(allchallenges.java:108)    import java.util.arraylist; import java.util.list;  import org.json.jsonarray; import org.json.jsonexception; import org.json.jsonobject;  import android.content.context; import android.os.bundle; import android.support.v4.app.fragment; import android.util.log;     import android.view.layoutinflater;     import android.view.view;     import android.view.viewgroup;     import android.widget.adapterview;     import android.widget.adapterview.onitemclicklistener;     import android.widget.button;     import android.widget.listview;     import android.widget.textview;      import com.loopj.android.http.jsonhttpresponsehandler;     import com.loopj.android.http.requestparams;      public class allchallenges extends fragment {       button loginbutton;     listview listview2, listview1;      textview textview;     int category = 0;     view view;     list<challenge> challenge_data;      public allchallenges(int cat) {         category = cat;     }      public allchallenges() {         // todo auto-generated constructor stub     }      public void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);      }      @override     public void onactivitycreated(bundle savedinstancestate) {         // todo auto-generated method stub         super.onactivitycreated(savedinstancestate);         if(getactivity()!=null){             load();         }     }       @override     public view oncreateview(layoutinflater inflater, viewgroup container,             bundle savedinstancestate) {         // todo auto-generated method stub          view = inflater.inflate(r.layout.allchallenges, container, false);          if(getactivity()!=null){             load();         }          return view;     }      public void load() {         loadold();          if (!appsettings.refreshed[category + 1]) {             update();         }      }      public void loadold() {          log.d("challenge", "updating " + category);         jsonarray = appsettings.loadchallenges(category, getactivity());         if (a != null) {              fitchallenges(makearray(a));         }      }      public void update() {         log.d("challenge", "getchallenges/" + category);         requestparams = new requestparams();         if (appsettings.logged) {             a.put("fb_id", appsettings.facebookid);             a.put("fb_authkey", appsettings.facebookauthtoken);         }         networkclient.receivejson("getchallenges/" + category, a,                 new jsonhttpresponsehandler() {                      @override                     public void onsuccess(jsonarray challist) {                         log.d("challenge", "downloaded " + category);                         appsettings.savechallenges(category, challist,                                 getactivity());                         fitchallenges(makearray(challist));                         appsettings.refreshed[category + 1] = true;                     }                 });      }      public challenge[] makearray(jsonarray list) {         challenge_data = new arraylist<challenge>();         try {             (int = 0; < list.length(); i++) {                 jsonobject chal = (jsonobject) list.get(i);                 string[] arr = {};                 if (chal.has("friends"))                     arr = jsonarrtoarr(chal.getjsonarray("friends"));                  log.e("boolean", chal.getstring("active"));                  challenge_data.add(new challenge(chal.getint("id"), chal                         .getstring("title"), chal.getint("category"), chal                         .getint("taken_by"), arr, chal.getboolean("active"),                         chal.getstring("text"), chal.getint("streak"), chal                                 .getint("percentage")));              }             return challenge_data.toarray(new challenge[challenge_data.size()]);         } catch (jsonexception e) {             // todo auto-generated catch block             e.printstacktrace();             return null;         }     }      public void fitchallenges(challenge[] chalarray) {         **challengeadapter adapter = new challengeadapter(getactivity(),                 r.layout.list_row, chalarray);** //         listview1 = (listview) view.findviewbyid(r.id.listview1);         listview1.setadapter(adapter);          onitemclicklistener listener = new onitemclicklistener() {             public void onitemclick(adapterview<?> parent, view view,                     int position, long id) {                  log.i("listener", "am id" + id + "position " + position);                 int n = (int) id;                 string title = challenge_data.get(n).tostringarray()[1];                 log.i("listener", "am id" + id + "position " + position                         + "  title " + title);                 int people = challenge_data.get(n).peopleint;                 string category = challenge_data.get(n).tostringarray()[0];                 string description = challenge_data.get(n).tostringarray()[5];                 int streak = challenge_data.get(n).streak;                 int percentage = challenge_data.get(n).percentage;                 int chal_id = challenge_data.get(n).id;                 boolean active = challenge_data.get(n).active;                  ((allchallengespager) getactivity()).showdetailed(chal_id,                         title, "" + people, category, active, description, ""                                 + streak, "" + percentage);             }         };          listview1.setonitemclicklistener(listener);      }      public string[] jsonarrtoarr(jsonarray arr) {         string[] newarr = {};         try {             list<string> list = new arraylist<string>();             (int = 0; < arr.length(); i++) {                  list.add(arr.getstring(i));              }             newarr = list.toarray(new string[list.size()]);         } catch (jsonexception e) {             // todo auto-generated catch block             e.printstacktrace();          }         return newarr;     }}  public class challengeadapter extends arrayadapter<challenge> {      context context;     int layoutresourceid;     challenge data[] = null;     typeface a;      public challengeadapter(context context, int layoutresourceid,             challenge[] data) {          **super(context, layoutresourceid, data);** //in line code crashes          this.layoutresourceid = layoutresourceid;         this.context = context;         this.data = data;         = typeface.createfromasset(context.getassets(),                 "fonts/roboto_condensed.ttf");      }       @override     public view getview(int position, view convertview, viewgroup parent) {         view row = convertview;         challengeholder holder = null;          challenge challenge = data[position];          if (row == null) {              layoutinflater inflater = ((activity) context).getlayoutinflater();             row = inflater.inflate(layoutresourceid, parent, false);             holder = new challengeholder();             holder.imgicon = (imageview) row.findviewbyid(r.id.list_image);             holder.txttitle = (textview) row.findviewbyid(r.id.challenge);             holder.txtpeople = (textview) row.findviewbyid(r.id.peoplenumber);             holder.friendthumbs = (linearlayout) row                     .findviewbyid(r.id.friendthumbs);              row.settag(holder);         } else {             holder = (challengeholder) row.gettag();         }         holder.friendthumbs.removeallviews();         (int = 0; < challenge.friendids.length; i++) {             holder.friendthumbs.addview(createicon(challenge.friendids[i],                     context));         }          holder.txttitle.settext(challenge.title);         holder.txttitle.settypeface(a);         holder.imgicon.setimageresource(challenge.icon);         holder.txtpeople.settext(challenge.peoplestring);          return row;     }      static class challengeholder {         public linearlayout friendthumbs;         imageview imgicon;         textview txttitle;         textview txtpeople;      }      public imageview createicon(string friendid, context cont) {         imageview icon = new imageview(context);         linearlayout.layoutparams par = new linearlayout.layoutparams(                 (int) cont.getresources().getdimension(r.dimen.fbiconsize),                 (int) cont.getresources().getdimension(r.dimen.fbiconsize));         par.setmargins(0, 0, 15, 0);         icon.setlayoutparams(par);          log.e("id " + friendid, "log");          final string myurl = appsettings.profilepictureurl(friendid);         icon.setimageresource(r.drawable.default_fb_icon_small);         ((myapplication) ((activity) context).getapplication())                 .loadimagesimple(myurl, icon);          return icon;      } } 

i guess cause of problem context equal null got method getactivity, don't know how deal problem. explain how solve problem?

most fragment detached activity @ stage called in external thread jsonhttpresponsehandler.

you have explanation here: https://stackoverflow.com/a/11536337/891479


Comments

Popular posts from this blog

matlab - Deleting rows with specific rules -

jquery - How would i go about shortening this code? And to cancel the previous click on click of new section? -