java - Custom adapter crash my app (NullPointerException) I'v tried every thing? -
i have been unable fix error; created new project , same result (nullpointerexception)
my adapter class:
public class contactadapter extends arrayadapter<contactsobject>{ private context context; private list<contactsobject> contactslist; public contactadapter(context context, list<contactsobject> list){ super(context,r.layout.ctninforow,list); this.context=context; this.contactslist=list; } public class viewholder{ textview tvname,tvemail,tvphone; public viewholder(view v){ textview tvname=(textview) v.findviewbyid(r.id.ctnname); textview tvemail=(textview) v.findviewbyid(r.id.ctnemail); textview tvphone=(textview) v.findviewbyid(r.id.ctnphone); } } @override public view getview(int position, view convertview, viewgroup parent) { viewholder myholder=null; if (convertview == null){ layoutinflater inflater =(layoutinflater) context.getsystemservice(context.layout_inflater_service); convertview = inflater.inflate(r.layout.ctninforow,parent,false); myholder=new viewholder(convertview); convertview.settag(myholder); }else{ myholder =(viewholder) convertview.gettag(); } // error goes here myholder.tvname.settext(contactslist.get(position).getname()); myholder.tvemail.settext(contactslist.get(position).getemail()); myholder.tvphone.settext(contactslist.get(position).getphone()); return convertview; } }
and xml file ctninforow layout :
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <textview android:layout_width="wrap_content" android:layout_height="wrap_content" android:textappearance="?android:attr/textappearancelarge" android:text="large text" android:id="@+id/ctnname" android:layout_gravity="center_horizontal" /> <textview android:layout_width="wrap_content" android:layout_height="wrap_content" android:textappearance="?android:attr/textappearancemedium" android:text="medium text" android:id="@+id/ctnemail" android:layout_gravity="center_horizontal" /> <textview android:layout_width="wrap_content" android:layout_height="wrap_content" android:textappearance="?android:attr/textappearancesmall" android:id="@+id/ctnphone" android:layout_gravity="center_horizontal"/> </linearlayout>
and logcat :
fatal exception: main process: com.example.mohammedriyadh.backendless1, pid: 2128 java.lang.nullpointerexception @ com.example.mriyadh.backendlessdemo.contactadapter.getview(contactadapter.java:69) @ android.widget.abslistview.obtainview(abslistview.java:2263) @ android.widget.listview.measureheightofchildren(listview.java:1263) @ android.widget.listview.onmeasure(listview.java:1175) @ android.view.view.measure(view.java:16497)
i did modification in code , works perfectly
public viewholder(view v) { textview tvname = (textview) v.findviewbyid(r.id.ctnname); textview tvemail = (textview) v.findviewbyid(r.id.ctnemail); textview tvphone = (textview) v.findviewbyid(r.id.ctnphone); }
into
public viewholder(view v) { tvname = (textview) v.findviewbyid(r.id.ctnname); tvemail = (textview) v.findviewbyid(r.id.ctnemail); tvphone = (textview) v.findviewbyid(r.id.ctnphone); }
also
layoutinflater inflater = (layoutinflater) context .getsystemservice(context.layout_inflater_service); convertview = inflater.inflate(r.layout.ctninforow, parent, false);
into
layoutinflater inflater=layoutinflater.from(getcontext()); convertview=inflater.inflate(r.layout.ctninforow,parent,false);
so complete code is
import android.content.context; import android.view.layoutinflater; import android.view.view; import android.view.viewgroup; import android.widget.arrayadapter; import android.widget.textview; import java.util.arraylist; import java.util.list; public class contactadapter extends arrayadapter<contactsobject> { private context context; private list<contactsobject> contactslist; public contactadapter(context context, arraylist<contactsobject> list) { // super(context,r.layout.ctninforow,list); super(context, 0, list); // super(context, 0, users); this.context = context; this.contactslist = list; } public class viewholder { textview tvname, tvemail, tvphone; public viewholder(view v) { tvname = (textview) v.findviewbyid(r.id.ctnname); tvemail = (textview) v.findviewbyid(r.id.ctnemail); tvphone = (textview) v.findviewbyid(r.id.ctnphone); } } @override public view getview(int position, view convertview, viewgroup parent) { viewholder myholder = null; if (convertview == null) { layoutinflater inflater = layoutinflater.from(getcontext()); convertview = inflater.inflate(r.layout.ctninforow, parent, false); myholder = new viewholder(convertview); convertview.settag(myholder); } else { myholder = (viewholder) convertview.gettag(); } myholder.tvname.settext(contactslist.get(position).name); myholder.tvemail.settext(contactslist.get(position).email); myholder.tvphone.settext(contactslist.get(position).phone); return convertview; }}
in mainactivity's oncreate():
protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); button b1 = (button)findviewbyid(r.id.button); assert b1 != null; b1.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { contactsobject newuser2 = new contactsobject("user1", "user1@gmail.com", "5145789076" ); adapter.add(newuser2); } }); arrayofusers = new arraylist<contactsobject>(); contactsobject user2 = new contactsobject("user2", "user2@gmail.com", "5145789076" ); arrayofusers.add(user2); adapter = new contactadapter(this, arrayofusers); listview listview = (listview) findviewbyid(android.r.id.list); listview.setadapter(adapter); }
Comments
Post a Comment