android - Trouble in ListFragment with BaseAdapter -
i trying create list custom baseadapter. getting nullpointerexception error, seem not load data adapter or view wrong.
below code:
fragmentactivity
public class newsitemlistactivity extends fragmentactivity implements newsitemlistfragment.callbacks { @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_newsitem_list); if (savedinstancestate != null) { mactiveposition = savedinstancestate.getint(state_active_position); } if (findviewbyid(r.id.newsitem_detail_container) != null) { listfragment = ((newsitemlistfragment) getsupportfragmentmanager() .findfragmentbyid(r.id.newsitem_list)); listfragment.setactivateonitemclick(true); } }
activity_newsitem_list layout
<fragment xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/newsitem_list" android:name="com.example.app.newsitemlistfragment" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_marginleft="16dp" android:layout_marginright="16dp" tools:context=".newsitemlistactivity" tools:layout="@android:layout/list_content" />
listfragment
public class newsitemlistfragment extends listfragment implements loadermanager.loadercallbacks<list<newsdata>>{ list<newsdata> listdata; customadapter mlistadapter; public newsitemlistfragment() { } @override public void onactivitycreated(bundle savedinstancestate) { super.onactivitycreated(savedinstancestate); // there no data setemptytext("no data here"); mlistadapter = new customadapter(getactivity(),listdata); setlistadapter(mlistadapter); // start out progress indicator. setlistshown(false); getloadermanager().initloader(0, null, this); } @override public loader<list<newsdata>> oncreateloader(int arg0, bundle arg1) { return new listloader(getactivity()); } @override public void onloadfinished(loader<list<newsdata>> arg0, list<newsdata> data) { listdata = data; mlistadapter.notifydatasetchanged(); if (isresumed()) { setlistshown(true); } else { setlistshownnoanimation(true); } } @override public void onloaderreset(loader<list<newsdata>> arg0) { listdata = null; } }
baseadapter
public class customadapter extends baseadapter{ private list<newsdata> list; context context; public customadapter(context c,list<newsdata> data){ context = c; list = data; } @override public int getcount() { return list.size(); } @override public object getitem(int position) { return list.get(position); } @override public long getitemid(int position) { return position; } @override public view getview(int position, view convertview, viewgroup parent) { view view; if (convertview == null) { layoutinflater minflater = (layoutinflater)context.getsystemservice(context.layout_inflater_service); view = minflater.inflate(r.layout.list_layout, parent, false); } else { view = convertview; } newsdata item = list.get(position); ((textview)view.findviewbyid(r.id.subject)).settext(item.getsubject()); ((textview)view.findviewbyid(r.id.cdate)).settext(item.getcdate()); return view; } }
newsdata class
public class newsdata { public string id; public string subject; public string cdate; public newsdata(string id, string subject,string cdate) { this.id = id; this.subject = subject; this.cdate = cdate; } public string getid() { return id; } public void setid(string id) { this.id = id; } public string getsubject() { return subject; } public void setsubject(string subject) { this.subject = subject; } ........ }
add loader
@override public list<newsdata> loadinbackground() { list<newsdata> entries = null; ..... return entries.add(new newsdata(i,subject,cdate)); } @override public void deliverresult(list<newsdata> listofdata) { list<newsdata> oldnews = listofdata; mnewsdata = listofdata; if (isstarted()) { // if loader started, can // deliver results. super.deliverresult(listofdata); } }
error log
04-05 07:31:17.951: e/androidruntime(1495): fatal exception: main 04-05 07:31:17.951: e/androidruntime(1495): java.lang.runtimeexception: unable start activity componentinfo{com.example.appapp/com.example.appapp.newsitemlistactivity}: java.lang.nullpointerexception 04-05 07:31:17.951: e/androidruntime(1495): @ android.app.activitythread.performlaunchactivity(activitythread.java:2059) 04-05 07:31:17.951: e/androidruntime(1495): @ android.app.activitythread.handlelaunchactivity(activitythread.java:2084) 04-05 07:31:17.951: e/androidruntime(1495): @ android.app.activitythread.access$600(activitythread.java:130) 04-05 07:31:17.951: e/androidruntime(1495): @ android.app.activitythread$h.handlemessage(activitythread.java:1195) 04-05 07:31:17.951: e/androidruntime(1495): @ android.os.handler.dispatchmessage(handler.java:99) 04-05 07:31:17.951: e/androidruntime(1495): @ android.os.looper.loop(looper.java:137) 04-05 07:31:17.951: e/androidruntime(1495): @ android.app.activitythread.main(activitythread.java:4745) 04-05 07:31:17.951: e/androidruntime(1495): @ java.lang.reflect.method.invokenative(native method) 04-05 07:31:17.951: e/androidruntime(1495): @ java.lang.reflect.method.invoke(method.java:511) 04-05 07:31:17.951: e/androidruntime(1495): @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:786) 04-05 07:31:17.951: e/androidruntime(1495): @ com.android.internal.os.zygoteinit.main(zygoteinit.java:553) 04-05 07:31:17.951: e/androidruntime(1495): @ dalvik.system.nativestart.main(native method) 04-05 07:31:17.951: e/androidruntime(1495): caused by: java.lang.nullpointerexception 04-05 07:31:17.951: e/androidruntime(1495): @ com.example.appapp.customadapter.getcount(customadapter.java:25) 04-05 07:31:17.951: e/androidruntime(1495): @ android.widget.listview.setadapter(listview.java:460) 04-05 07:31:17.951: e/androidruntime(1495): @ android.support.v4.app.listfragment.setlistadapter(listfragment.java:182) 04-05 07:31:17.951: e/androidruntime(1495): @ com.example.appapp.newsitemlistfragment.onactivitycreated(newsitemlistfragment.java:96) 04-05 07:31:17.951: e/androidruntime(1495): @ android.support.v4.app.fragment.performactivitycreated(fragment.java:1468) 04-05 07:31:17.951: e/androidruntime(1495): @ android.support.v4.app.fragmentmanagerimpl.movetostate(fragmentmanager.java:931) 04-05 07:31:17.951: e/androidruntime(1495): @ android.support.v4.app.fragmentmanagerimpl.movetostate(fragmentmanager.java:1088) 04-05 07:31:17.951: e/androidruntime(1495): @ android.support.v4.app.fragmentmanagerimpl.movetostate(fragmentmanager.java:1070) 04-05 07:31:17.951: e/androidruntime(1495): @ android.support.v4.app.fragmentmanagerimpl.dispatchactivitycreated(fragmentmanager.java:1861) 04-05 07:31:17.951: e/androidruntime(1495): @ android.support.v4.app.fragmentactivity.onstart(fragmentactivity.java:547) 04-05 07:31:17.951: e/androidruntime(1495): @ android.app.instrumentation.callactivityonstart(instrumentation.java:1163) 04-05 07:31:17.951: e/androidruntime(1495): @ android.app.activity.performstart(activity.java:5018) 04-05 07:31:17.951: e/androidruntime(1495): @ android.app.activitythread.performlaunchactivity(activitythread.java:2032) 04-05 07:31:17.951: e/androidruntime(1495): ... 11 more
sorry long post...
its quite simple ,
i can see have not initialize list<newsdata> listdata
why getting nullpointerexception
.
you should create new instance of list before use .
write listdata = new list<newsdata> ()
in oncreate ()
, add item in list
Comments
Post a Comment