android - how to bind list adapter in fragment class -
i have following fragment class:
public class home extends fragment { public static final string tag = home.class.getsimplename(); public static homenewinstance() { return new home(); } @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); } @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { view v = inflater.inflate(r.layout.homegrag, container, false); return v; } }
when used work perferct. when add below code in class give me error :
public class home extends fragment { private list<rowitem> rowitems; private static integer[] images = { r.drawable.red, r.drawable.red, r.drawable.red, r.drawable.red, r.drawable.red, r.drawable.red, r.drawable.red, r.drawable.red }; public static final string tag = home.class.getsimplename(); public static homenewinstance() { return new home(); } @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); } @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { view v = inflater.inflate(r.layout.homegrag, container, false); listview lv = (listview) getactivity().findviewbyid(r.id.mylist); rowitems = new arraylist<rowitem>(); string[] titles = { "movie1", "movie2", "movie3", "movie4", "movie5", "movie6", "movie7", "movie8" }; string[] descriptions = { "first movie", "second movie", "third movie", "fourth movie", "fifth movie", "sixth movie", "seventh movie", "eighth movie" }; // populate list (int = 0; < titles.length; i++) { rowitem item = new rowitem(images[i], titles[i], descriptions[i]); rowitems.add(item); } // set adapter on listview lazyadapter adapter = new lazyadapter(getactivity() .getapplicationcontext(), r.layout.list_row, rowitems); lv.setadapter(adapter); lv.setonitemselectedlistener(new onitemselectedlistener() { @override public void onitemselected(adapterview<?> arg0, view arg1, int arg2, long arg3) { // todo auto-generated method stub } @override public void onnothingselected(adapterview<?> arg0) { // todo auto-generated method stub } }); return v; } }
then give me error @ lv.setadapter(adapter);
logcat :
fatal exception: main process: com.paulusworld.drawernavigationtabs, pid: 11867 java.lang.nullpointerexception @ com.paulusworld.drawernavigationtabs.rush.oncreateview(rush.java:55) @ android.support.v4.app.fragment.performcreateview(fragment.java:1500) @ android.support.v4.app.fragmentmanagerimpl.movetostate(fragmentmanager.java:927) @ android.support.v4.app.fragmentmanagerimpl.movetostate(fragmentmanager.java:1104) @ android.support.v4.app.backstackrecord.run(backstackrecord.java:682) @ android.support.v4.app.fragmentmanagerimpl.execpendingactions(fragmentmanager.java:1467) @ android.support.v4.app.fragmentmanagerimpl$1.run(fragmentmanager.java:440) @ android.os.handler.handlecallback(handler.java:808) @ android.os.handler.dispatchmessage(handler.java:103) @ android.os.looper.loop(looper.java:193) @ android.app.activitythread.main(activitythread.java:5333) @ java.lang.reflect.method.invokenative(native method) @ java.lang.reflect.method.invoke(method.java:515) @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:824) @ com.android.internal.os.zygoteinit.main(zygoteinit.java:640) @ dalvik.system.nativestart.main(native method)
how can ? want bind list lazyadapter.
change
listview lv = (listview) getactivity().findviewbyid(r.id.mylist);
to
listview lv = (listview) v.findviewbyid(r.id.mylist);
getactivity()
represents fragment
's current context while returned view represents reference initialize view
's.
Comments
Post a Comment