java - Android: I'm confused about how to get the right context to find my Views -


i've got maintabbedactivity.xml holds fragment.xml. fragment.xml holds listview. listview gets populated array of customview.xml. customview.xml holds textview wanna pass string to.

so it's like:

maintabbedactivity.xml -fragment.xml --customview.xml 

i create new customviews inside of fragment.java , populate listview them. working fine. try find textview set text, crashes because null. i'm pretty new android, after day of googleing looks i'm passing wrong context. don't know how right one.

this how pass context in fragment.java:

customview newcv = new customview (getactivity.getapplicationcontext()); newcv.setname("hello"); 

here's customview.java looks like, including line returns null:

public class customview extends framelayout{      string name;     //views     textview usernametoset;      public string getname(){         return name;     }      public void setname(string name){         this.name = name;     }      public customview(context context){         super(context);          usernametoset = (textview)findviewbyid(r.id.username); //this returns null...         usernametoset.settext(getname()); //...and crashes because of     }  } 

thanks :)

edit: here fragment.java creates customviews

    public class statusfragment extends fragment {         listview listview;          customview cvcollection[];          public static final mediatype json                 = mediatype.parse("application/json; charset=utf-8");          okhttpclient client = new okhttpclient();          /**          * fragment argument representing section number          * fragment.          */         private static final string arg_section_number = "section_number";          /**          * returns new instance of fragment given section          * number.          */         public static statusfragment newinstance(int sectionnumber) {             statusfragment fragment = new statusfragment();             bundle args = new bundle();             args.putint(arg_section_number, sectionnumber);             fragment.setarguments(args);             return fragment;         }          public statusfragment() {         }          @override         public view oncreateview(layoutinflater inflater, viewgroup container,                                  bundle savedinstancestate) {             view rootview = inflater.inflate(r.layout.fragment_status_tabbed, container, false);              //get statuslist server             getstatuslistfromserver(rootview);              return rootview;         }             private void getstatuslistfromserver(final view inflatedview){              //json service *********************************             string json = "{'userid' : '1'}";             requestbody body = requestbody.create(json,json);             request request = new request.builder()                     .url("example.com")                     .post(body)                     .build();             call call = client.newcall(request);             call.enqueue(new callback() {                 @override                 public void onfailure(request request, ioexception e) {                  }                  @override                 public void onresponse(response response) throws ioexception {                      if (response.issuccessful()) {                         try {                             string jsonstring = response.body().string();                              jsonarray jarray = new jsonarray(jsonstring);                             cvcollection = new customview[jarray.length()];                              (int i=0; i<jarray.length() -1; i++){                                 final jsonobject statusrow = jarray.getjsonobject(i);                                  customview newcv = new customview(inflatedview.getcontext());                                 newcv.setname(statusrow.getstring("userid"));                                 newcv.setstatus(statusrow.getstring("text"));                                 newcv.seterstellt(statusrow.getstring("erstellt"));                                 newcv.setfavorite(false);                                  cvcollection[i] = newcv;                             }                             //put them list                             filllist(inflatedview, cvcollection);                         }catch(jsonexception ex){                          }                         //log.w("json***************", response.body().string());                     } else{                         toast.maketext(getactivity(), "error", toast.length_short).show();                     }                 }             }); }  public void filllist(view inflatedview, customview[] statslist){             listview = (listview) inflatedview.findviewbyid(r.id.statuslist);              //get stats             customview customview_data[] = statslist;              //load them adapter             customviewadapter adapter = new customviewadapter(getactivity(), r.layout.customview, customview_data);              //load adapter listview             listview.setadapter(adapter);          }         }       } 

your code not complete, i'm not sure, think need inflate custom view somewhere. see, you're not doing that.

something like:

view view= getlayoutinflater().inflate(r.layout.customview, null); usernametoset = (textview)view.findviewbyid(r.id.username); 

Comments

Popular posts from this blog

javascript - Chart.js (Radar Chart) different scaleLineColor for each scaleLine -

apache - Error with PHP mail(): Multiple or malformed newlines found in additional_header -

java - Android – MapFragment overlay button shadow, just like MyLocation button -