how to fill custom list in Fragment in android? -
i have fragment want create custom list view in when send parameters adapter it's giving me error.
this code in fragment..
@override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { view v = inflater.inflate(r.layout.fragment_list, container, false); textview tv = (textview) v.findviewbyid(r.id.textview1); customadapter arrayadapter = new customadapter(getactivity(), r.layout.search_catagory_list, prgmimages, prgmnamelist); lv = (listview) v.findviewbyid(r.id.listview); lv.setadapter(arrayadapter); return v; }
this mainhome.java
package com.example.yasee.bitescene.fragments; import android.content.context; import android.net.uri; import android.os.bundle; import android.app.fragment; import android.view.layoutinflater; import android.view.view; import android.view.viewgroup; import android.widget.listview; import android.widget.textview; import com.example.yasee.bitescene.r; import com.example.yasee.bitescene.adapter.customadapter; import java.util.arraylist; public class mainhome extends fragment { private static final string arg_param1 = "param1"; private static final string arg_param2 = "param2"; private string mparam1; private string mparam2; listview lv; public static int [] prgmimages={r.drawable.one,r.drawable.two,r.drawable.three,r.drawable.four,r.drawable.five}; public static string [] prgmnamelist={"truly random","tex mex","bbq","brunch","sushi"}; private onfragmentinteractionlistener mlistener; public mainhome() { } @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { view v = inflater.inflate(r.layout.fragment_list, container, false); textview tv = (textview) v.findviewbyid(r.id.textview1); customadapter arrayadapter = new customadapter(getactivity(), r.layout.search_catagory_list, prgmimages, prgmnamelist); lv = (listview) v.findviewbyid(r.id.listview); lv.setadapter(arrayadapter); return v; } public static mainhome newinstance(string param1, string param2) { mainhome fragment = new mainhome(); bundle args = new bundle(); args.putstring(arg_param1, param1); args.putstring(arg_param2, param2); fragment.setarguments(args); return fragment; } @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); if (getarguments() != null) { mparam1 = getarguments().getstring(arg_param1); mparam2 = getarguments().getstring(arg_param2); } } public void onbuttonpressed(uri uri) { if (mlistener != null) { mlistener.onfragmentinteraction(uri); } } @override public void onattach(context context) { super.onattach(context); if (context instanceof onfragmentinteractionlistener) { mlistener = (onfragmentinteractionlistener) context; } else { throw new runtimeexception(context.tostring() + " must implement onfragmentinteractionlistener"); } } @override public void ondetach() { super.ondetach(); mlistener = null; } /** * interface must implemented activities contain * fragment allow interaction in fragment communicated * activity , potentially other fragments contained in * activity. * <p> * see android training lesson <a href= * "http://developer.android.com/training/basics/fragments/communicating.html" * >communicating other fragments</a> more information. */ public interface onfragmentinteractionlistener { // todo: update argument type , name void onfragmentinteraction(uri uri); } }
it show error this..
this adapter code...
package com.example.yasee.bitescene.adapter; import android.app.activity; import android.content.context; import android.view.layoutinflater; import android.view.view; import android.view.viewgroup; import android.widget.baseadapter; import android.widget.imageview; import android.widget.textview; import android.widget.toast; import com.example.yasee.bitescene.fragments.mainhome; import com.example.yasee.bitescene.r; import com.example.yasee.bitescene.search; import java.util.arraylist; public class customadapter extends baseadapter { string [] result; context context; int [] imageid; private static layoutinflater inflater=null; public customadapter(activity activity, int search_catagory_list, int[] prgmimages, string[] prgmnamelist) { // todo auto-generated constructor stub result=prgmnamelist; context=activity; imageid=prgmimages; inflater = ( layoutinflater )context.getsystemservice(context.layout_inflater_service); } @override public int getcount() { // todo auto-generated method stub return result.length; } @override public object getitem(int position) { // todo auto-generated method stub return position; } @override public long getitemid(int position) { // todo auto-generated method stub return position; } public class holder { textview tv; imageview img; } @override public view getview(final int position, view convertview, viewgroup parent) { // todo auto-generated method stub holder holder=new holder(); view rowview; rowview = inflater.inflate(r.layout.search_catagory_list, null); holder.tv= (textview) rowview.findviewbyid(r.id.textview1); holder.img=(imageview) rowview.findviewbyid(r.id.imageview1); holder.tv.settext(result[position]); holder.img.setimageresource(imageid[position]); rowview.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { toast.maketext(context, "you clicked "+result[position], toast.length_long).show(); } }); return rowview; } }
my error is...
06-13 07:31:00.399 3569-3569/com.example.yasee.bitescene e/androidruntime: fatal exception: main process: com.example.yasee.bitescene, pid: 3569 java.lang.runtimeexception: unable start activity componentinfo{com.example.yasee.bitescene/com.example.yasee.bitescene.search}: java.lang.nullpointerexception @ android.app.activitythread.performlaunchactivity(activitythread.java:2195) @ android.app.activitythread.handlelaunchactivity(activitythread.java:2245) @ android.app.activitythread.access$800(activitythread.java:135) @ android.app.activitythread$h.handlemessage(activitythread.java:1196) @ android.os.handler.dispatchmessage(handler.java:102) @ android.os.looper.loop(looper.java:136) @ android.app.activitythread.main(activitythread.java:5017) @ java.lang.reflect.method.invokenative(native method) @ java.lang.reflect.method.invoke(method.java:515) @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:779) @ com.android.internal.os.zygoteinit.main(zygoteinit.java:595) @ dalvik.system.nativestart.main(native method) caused by: java.lang.nullpointerexception @ com.example.yasee.bitescene.fragments.mainhome.oncreateview(mainhome.java:44) @ android.app.fragment.performcreateview(fragment.java:1700) @ android.app.fragmentmanagerimpl.movetostate(fragmentmanager.java:890) @ android.app.fragmentmanagerimpl.movetostate(fragmentmanager.java:1062) @ android.app.backstackrecord.run(backstackrecord.java:684) @ android.app.fragmentmanagerimpl.execpendingactions(fragmentmanager.java:1447) @ android.app.activity.performstart(activity.java:5240) @ android.app.activitythread.performlaunchactivity(activitythread.java:2168) @ android.app.activitythread.handlelaunchactivity(activitythread.java:2245) @ android.app.activitythread.access$800(activitythread.java:135) @ android.app.activitythread$h.handlemessage(activitythread.java:1196) @ android.os.handler.dispatchmessage(handler.java:102) @ android.os.looper.loop(looper.java:136) @ android.app.activitythread.main(activitythread.java:5017) @ java.lang.reflect.method.invokenative(native method) @ java.lang.reflect.method.invoke(method.java:515) @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:779) @ com.android.internal.os.zygoteinit.main(zygoteinit.java:595) @ dalvik.system.nativestart.main(native method)
lv
appears null leading nullpointerexception
.
make sure id
for listview
set listview
expected in line below. make sure inside layout fragment_list
lv = (listview) v.findviewbyid(r.id.listview);
Comments
Post a Comment