CustomListViewAddapter in Fragment Android -
i have problem customlistviewaddapter in fragment. have main activity has slide menu , try show customlistview in it. , error when tried use try-catch
here part of main activity call fragment slide menu
@suppresslint("newapi")public class mainactivity extends activity { .......some code..... @targetapi(build.version_codes.honeycomb) @suppresslint("newapi") private void displayview(int position) { log.i("position***", position + ""); // update main content replacing fragments fragment fragment = null; fragment fragment1 = null; switch (position) { case 0: fragment = new homefragment(); fragment1 = fragment; log.i("fragment1", fragment1 + ""); break; case 1: fragment = new findpeoplefragment(); break; case 2: fragment = new photosfragment(); break; case 3: fragment = new communityfragment(); break; case 4: fragment = new pagesfragment(); break; case 5: fragment = new whatshotfragment(); break; default: break; } if (fragment != null) { fragmentmanager fragmentmanager = getfragmentmanager(); fragmentmanager.begintransaction() .replace(r.id.frame_container, fragment).commit(); // update selected item , title, close drawer mdrawerlist.setitemchecked(position, true); mdrawerlist.setselection(position); settitle(navmenutitles[position]); mdrawerlayout.closedrawer(mdrawerlist); } else { // error in creating fragment log.e("mainactivity", "error in creating fragment"); } } code }
he homefragment.java
public class homefragment extends listfragment implements onclicklistener { @override public void onactivitycreated(bundle savedinstancestate) { // todo auto-generated method stub super.onactivitycreated(savedinstancestate); } @override public view oncreateview(layoutinflater inflater, viewgroup container,bundle savedinstancestate) { // inflate layout fragment view fragprocatview = inflater.inflate(r.layout.fragment_home, container,false); mainlistview = (listview) fragprocatview.findviewbyid(r.id.list); /* * listactivity has default layout consists of single, * full-screen list in center of screen. however,we can * customize screen layout setting our own view layout * setcontentview() in oncreate(). this, our layout must contain * listview object id "@android:id/list". ( not * android:id="@+id/list" ). if define listview android:id * attribute set @+id/list throw below runtimeexception */ datasource = new etpupdatasource(getactivity()); datasource.open(); addedetpup = datasource.getallrecordlist(); datasource.close(); log.i("dbcount", addedetpup.size() + ""); // if first time run app maybe dont have data // on need check // , call our list view again if (addedetpup.size() == 0) { if (internetstatus.getinstance(getactivity()).havenetworkconnection(getactivity())) { log.i("xxxx return haveconnectedwifi", "return haveconnectedwifixxxx"); alertdialog.builder alertbuilder = new alertdialog.builder(getactivity()); alertbuilder.setmessage("there no dara show. want add new data?"); alertbuilder.setcancelable(false); alertbuilder.setpositivebutton("yes", new dialoginterface.onclicklistener() { public void onclick(dialoginterface dialog, int which) { // call json data server new jsonparse().execute(); refreshlist(); } }); alertbuilder.setnegativebutton("no", new dialoginterface.onclicklistener() { public void onclick(dialoginterface dialog, int which) { dialog.cancel(); } }); alertdialog alert = alertbuilder.create(); alert.show(); } else { toast.maketext( getactivity(), "there no internet connection available, please make sure device connected internet via wifi or mobile internet!", toast.length_long).show(); } } // create new object customlistviewaddapter , pass // context , list of etpup data try{ arrayadapter<getalldata> adaptermain = new customlistviewaddapter(getactivity(), addedetpup); mainlistview.setadapter(adaptermain); //mainlistview.setadapter(new customlistviewaddapter(getactivity(), addedetpup)); }catch(exception e){ log.e("error happened ", "during creating list view"); log.e("error happened ", e.tostring()); } // registerforcontextmenu(mainlistview.getlistview()); btngetdata = (button) getview().findviewbyid(r.id.getdata); btngetdata.setonclicklistener(this); btnshowfav = (button) getview().findviewbyid(r.id.showfav); btnshowfav.setonclicklistener(this); return inflater.inflate(r.layout.fragment_home, container, false); } @override public void onclick(view v) { switch (v.getid()) { case r.id.getdata: new jsonparse().execute(); case r.id.showfav: log.i("show fav", "in show fav btn"); intent showfav = new intent(getactivity(), showfavorite.class); startactivity(showfav); break; } } public void refreshlist() { log.i("refresh list", "i'm in refresh list"); // create new object customlistviewaddapter , pass // context , list of etpup data datasource.open(); addedetpup = datasource.getallrecordlist(); datasource.close(); log.i("dbcount*****00******", addedetpup.size() + ""); try{ arrayadapter<getalldata> adaptermain = new customlistviewaddapter(getactivity(), addedetpup); mainlistview.setadapter(adaptermain); }catch(exception e){ log.e("error happened in refresh", "during creating list view"); log.e("error happened ", e.tostring()); } // registerforcontextmenu(((listactivity) mainlistview).getlistview()); // show simple list /* * arrayadapter<getalldata> adapter = new arrayadapter<getalldata>(this, * android.r.layout.simple_expandable_list_item_1,addedzekrfields2); * setlistadapter(adapter); registerforcontextmenu(getlistview()); */ }
and here log cat
06-01 23:24:38.861: d/dalvikvm(1873): gc_for_alloc freed 96k, 6% free 3142k/3308k, paused 30ms, total 32ms blockquote 06-01 23:24:38.871: i/dalvikvm-heap(1873): grow heap (frag case) 4.207mb 1127536-byte allocation 06-01 23:24:38.911: d/dalvikvm(1873): gc_for_alloc freed 2k, 4% free 4240k/4412k, paused 31ms, total 31ms 06-01 23:24:39.021: i/position***(1873): 2 06-01 23:24:39.331: d/gralloc_goldfish(1873): emulator without gpu emulation detected. 06-01 23:24:42.801: i/in draw click***(1873): 000000000000011123 06-01 23:24:42.801: i/position***(1873): 0 06-01 23:24:42.811: i/fragment1(1873): homefragment{b1f98308} 06-01 23:24:43.061: d/dalvikvm(1873): gc_for_alloc freed 80k, 4% free 4657k/4816k, paused 24ms, total 29ms 06-01 23:24:43.121: i/etpup(1873): database open 06-01 23:24:43.141: i/etpup(1873): database close 06-01 23:24:43.141: i/dbcount(1873): 3 06-01 23:24:43.161: e/error happened(1873): during creating list view 06-01 23:24:43.161: e/error happened(1873): java.lang.nullpointerexception 06-01 23:24:43.161: d/androidruntime(1873): shutting down vm 06-01 23:24:43.161: w/dalvikvm(1873): threadid=1: thread exiting uncaught exception (group=0xb1b08ba8) 06-01 23:24:43.181: e/androidruntime(1873): fatal exception: main 06-01 23:24:43.181: e/androidruntime(1873): process: com.pishgamanit.etpup, pid: 1873 06-01 23:24:43.181: e/androidruntime(1873): java.lang.nullpointerexception 06-01 23:24:43.181: e/androidruntime(1873): @ com.pishgamanit.etpup.homefragment.oncreateview(homefragment.java:171) 06-01 23:24:43.181: e/androidruntime(1873): @ android.app.fragment.performcreateview(fragment.java:1700) 06-01 23:24:43.181: e/androidruntime(1873): @ android.app.fragmentmanagerimpl.movetostate(fragmentmanager.java:890) 06-01 23:24:43.181: e/androidruntime(1873): @ android.app.fragmentmanagerimpl.movetostate(fragmentmanager.java:1062) 06-01 23:24:43.181: e/androidruntime(1873): @ android.app.backstackrecord.run(backstackrecord.java:684) 06-01 23:24:43.181: e/androidruntime(1873): @ android.app.fragmentmanagerimpl.execpendingactions(fragmentmanager.java:1447) 06-01 23:24:43.181: e/androidruntime(1873): @ android.app.fragmentmanagerimpl$1.run(fragmentmanager.java:443) 06-01 23:24:43.181: e/androidruntime(1873): @ android.os.handler.handlecallback(handler.java:733) 06-01 23:24:43.181: e/androidruntime(1873): @ android.os.handler.dispatchmessage(handler.java:95) 06-01 23:24:43.181: e/androidruntime(1873): @ android.os.looper.loop(looper.java:136) 06-01 23:24:43.181: e/androidruntime(1873): @ android.app.activitythread.main(activitythread.java:5017) 06-01 23:24:43.181: e/androidruntime(1873): @ java.lang.reflect.method.invokenative(native method) 06-01 23:24:43.181: e/androidruntime(1873): @ java.lang.reflect.method.invoke(method.java:515) 06-01 23:24:43.181: e/androidruntime(1873): @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:779) 06-01 23:24:43.181: e/androidruntime(1873): @ com.android.internal.os.zygoteinit.main(zygoteinit.java:595) 06-01 23:24:43.181: e/androidruntime(1873): @ dalvik.system.nativestart.main(native method) 06-01 23:29:43.301: i/process(1873): sending signal. pid: 1873 sig: 9
here main xml
<android.support.v4.widget.drawerlayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/drawer_layout" android:layout_width="match_parent" android:layout_height="match_parent" > <framelayout android:id="@+id/frame_container" android:layout_width="match_parent" android:layout_height="match_parent" /> <!-- listview display slider menu --> <listview android:id="@+id/list_slidermenu" android:layout_width="240dp" android:layout_height="match_parent" android:layout_gravity="start" android:background="@color/list_background" android:choicemode="singlechoice" android:divider="@color/list_divider" android:dividerheight="1dp" android:listselector="@drawable/list_selector" />
and here fragment_home.xml
<?xml version="1.0" encoding="utf-8"?> <relativelayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/drawer_layout1111111111111" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingbottom="@dimen/activity_vertical_margin" android:paddingleft="@dimen/activity_horizontal_margin" android:paddingright="@dimen/activity_horizontal_margin" android:paddingtop="@dimen/activity_vertical_margin" > <listview android:id="@android:id/list" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_above="@+id/getdata" android:layout_alignparentleft="true" /> <button android:id="@+id/getdata" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignbaseline="@+id/showfav" android:layout_alignbottom="@+id/showfav" android:layout_alignleft="@+id/list" android:text="get data" /> <button android:id="@+id/showfav" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignparentbottom="true" android:layout_alignright="@android:id/list" android:layout_marginbottom="14dp" android:layout_marginright="16dp" android:text="show favorites" /> </relativelayout>
i did lot of researches , i'm stuck here week :(
i appricate , advice
first correct should
return fragprocatview;
instead of
return inflater.inflate(r.layout.fragment_home, container, false);
in oncreateview(.....)
in homefragment
update:
first remove listfragment
, extends simple fragment
homefragment
your should change android:id="@android:id/list"
android:id="@+id/list"
and should change this
btngetdata = (button) getview().findviewbyid(r.id.getdata) btnshowfav = (button) getview().findviewbyid(r.id.showfav);
to
btngetdata = (button) fragprocatview.findviewbyid(r.id.getdata) btnshowfav = (button) fragprocatview.findviewbyid(r.id.showfav);
Comments
Post a Comment