Android App Crashing Depending on how Array of Objects is declared? -


thanks reading , helping out! appreciate it. on 1 of activities in app have list view, supposed display array of custom type "memberlistobject" have custom memberlistobjectadapter class. code works great when delcare array such(in oncreate method). list displays how want too

memberlistobject[] memberlistobject_data = new memberlistobject[]{     new memberlistobject(r.drawable.icon_pin,"chocolate") }; memberlistobjectadapter adapter = new memberlistobjectadapter(this,              r.layout.listview_item_row, memberlistobject_data); listview1.setadapter(adapter); 

however when delcare array this

memberlistobject[] memberlistobject_data= new memberlistobject[5];  memberlistobject_data[0].icon=(r.drawable.icon_pin);   memberlistobject_data[0].title="chocolate";  memberlistobjectadapter adapter = new memberlistobjectadapter(this,              r.layout.listview_item_row, memberlistobject_data); listview1.setadapter(adapter); 

my app crashes when activity ran, has been confusing me while.

here "memberlistobjectadapter.java"

public class memberlistobjectadapter extends arrayadapter<memberlistobject>{  context context;  int layoutresourceid;     memberlistobject data[] = null;  public memberlistobjectadapter(context context, int layoutresourceid, memberlistobject[] data) {     super(context, layoutresourceid, data);     this.layoutresourceid = layoutresourceid;     this.context = context;     this.data = data; }  @override public view getview(int position, view convertview, viewgroup parent) {     view row = convertview;     weatherholder holder = null;      if(row == null)     {         layoutinflater inflater = ((activity)context).getlayoutinflater();         row = inflater.inflate(layoutresourceid, parent, false);          holder = new weatherholder();         holder.imgicon = (imageview)row.findviewbyid(r.id.imgicon);         holder.txttitle = (textview)row.findviewbyid(r.id.txttitle);          row.settag(holder);     }     else     {         holder = (weatherholder)row.gettag();     }      memberlistobject memberlistobject = data[position];     holder.txttitle.settext(memberlistobject.title);     holder.imgicon.setimageresource(memberlistobject.icon);      return row; }  static class weatherholder {     imageview imgicon;     textview txttitle; } } 

so question is, why might app crashing when declare/use 2nd way not first way? alot!

when create array, members not created automatically. have yourself.

memberlistobject[] memberlistobject_data= new memberlistobject[5]; memberlistobject_data[0] = new memberlistobject(); memberlistobject_data[1] = new memberlistobject(); memberlistobject_data[2] = new memberlistobject(); memberlistobject_data[3] = new memberlistobject(); memberlistobject_data[4] = new memberlistobject(); 

or loop

for(int = 0; < memberlistobject_data.length; i++){     memberlistobject_data[i] = new memberlistobject(); } 

Comments

Popular posts from this blog

how to proxy from https to http with lighttpd -

android - Automated my builds -

python - Flask migration error -