indexoutofboundsexception - android:array index out of bound exception -
import android.app.activity; import android.app.fragment; import android.app.fragmentmanager; import android.content.res.configuration; import android.content.res.typedarray; import android.os.bundle; import android.support.v4.widget.drawerlayout; import android.support.v4.app.actionbardrawertoggle; import android.util.log; import android.view.menu; import android.view.menuitem; import android.view.view; import android.widget.adapterview; import android.widget.listview; import java.util.arraylist; public class mainactivity extends activity { private drawerlayout drawerlayout; private listview lvslidingmenu; private actionbardrawertoggle drawertoggle; // navigation drawer titles private charsequence drawertitle; private charsequence apptitle; // sliding menu items private string[] titles; private typedarray icons; private arraylist slidingmenuitems; private slidingmenuadapter adapter; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); apptitle = drawertitle = gettitle(); // load resources titles = getresources().getstringarray(r.array.nav_drawer_items); icons = getresources().obtaintypedarray(r.array.nav_drawer_icons); // sliding menu listview istance drawerlayout = (drawerlayout) findviewbyid(r.id.drawer_layout); lvslidingmenu = (listview) findviewbyid(r.id.lv_sliding_menu); slidingmenuitems = new arraylist(); // creating , adding slidingmenuitems slidingmenuitems.add(new slidingmenuitem(titles[0], icons.getresourceid(0,-1))); slidingmenuitems.add(new slidingmenuitem(titles[1], icons.getresourceid(1, -1))); slidingmenuitems.add(new slidingmenuitem(titles[2], icons.getresourceid(2, -1))); slidingmenuitems.add(new slidingmenuitem(titles[3], icons.getresourceid(3, -1))); // recycle typed array icons.recycle(); lvslidingmenu.setonitemclicklistener(new slidemenuclicklistener()); // assign adapter listview adapter = new slidingmenuadapter(getapplicationcontext(), slidingmenuitems); lvslidingmenu.setadapter(adapter); // enable action bar app icon , behaving toggle button getactionbar().setdisplayhomeasupenabled(true); getactionbar().sethomebuttonenabled(true); drawertoggle = new actionbardrawertoggle(this, drawerlayout, r.drawable.ic_drawer // navigation drawer , r.string.app_name, //navigation drawer open - description accessibility r.string.app_name) // navigation drawer close - description accessibility { public void ondrawerclosed(view view) { getactionbar().settitle(apptitle); invalidateoptionsmenu(); } public void ondraweropened(view drawerview) { getactionbar().settitle(drawertitle); invalidateoptionsmenu(); } }; drawerlayout.setdrawerlistener(drawertoggle); if (savedinstancestate == null) { // on first time, show home fragment displayview(0); } } /** * slide menu item click listener * */ private class slidemenuclicklistener implements listview.onitemclicklistener { @override public void onitemclick(adapterview<?> parent, view view, int position, long id) { // display appropriate fragment selected item displayview(position); } } @override public boolean oncreateoptionsmenu(menu menu) { getmenuinflater().inflate(r.menu.menu_main, menu); return true; } @override public boolean onoptionsitemselected(menuitem item) { // toggle navigation drawer on selecting action bar app icon/title if (drawertoggle.onoptionsitemselected(item)) { return true; } // handle action bar actions click switch (item.getitemid()) { case r.id.action_settings: return true; default: return super.onoptionsitemselected(item); } } /* * * called when invalidateoptionsmenu() triggered */ @override public boolean onprepareoptionsmenu(menu menu) { // if navigation drawer opened, hide action items boolean draweropen = drawerlayout.isdraweropen(lvslidingmenu); menu.finditem(r.id.action_settings).setvisible(!draweropen); return super.onprepareoptionsmenu(menu); } /** * display fragment view selected navigation drawer list item * */ private void displayview(int position) { fragment fragment = null; switch (position) { case 0: fragment = new homefragment(); break; default: break; } if (fragment != null) { fragmentmanager fragmentmanager = getfragmentmanager(); fragmentmanager.begintransaction().replace(r.id.fragment_detail, fragment).commit(); // update selected item , title, close drawer lvslidingmenu.setitemchecked(position, true); lvslidingmenu.setselection(position); settitle(titles[position]); drawerlayout.closedrawer(lvslidingmenu); } else { // log error log.e("mainactivity", "error in creating fragment"); } } @override public void settitle(charsequence title) { apptitle = title; getactionbar().settitle(apptitle); } /** * when using actionbardrawertoggle, must call during * onpostcreate() , onconfigurationchanged()... */ @override protected void onpostcreate(bundle savedinstancestate) { super.onpostcreate(savedinstancestate); // sync toggle state after onrestoreinstancestate has occurred. drawertoggle.syncstate(); } @override public void onconfigurationchanged(configuration newconfig) { super.onconfigurationchanged(newconfig); // pass configuration change drawer toggls drawertoggle.onconfigurationchanged(newconfig); } }
this mainactivty.java
public class slidingmenuadapter extends baseadapter { private context context; private arraylist<slidingmenuitem> items; public slidingmenuadapter(context context, arraylist<slidingmenuitem> items) { this.context = context; this.items = items; } @override public int getcount() { return items.size(); } @override public object getitem(int index) { return items.get(index); } @override public long getitemid(int index) { return index; } @override public view getview(int index, view view, viewgroup arg2) { if (view == null) { layoutinflater minflater = (layoutinflater) context.getsystemservice(activity.layout_inflater_service); view = minflater.inflate(r.layout.iv_item_sliding_menu, null); } imageview imgicon = (imageview) view.findviewbyid(r.id.img_sliding_menu_item); textview txttitle = (textview) view.findviewbyid(r.id.tv_sliding_menu_item); slidingmenuitem item = items.get(index); imgicon.setimageresource(item.geticon()); txttitle.settext(item.gettitle()); return view; } }
this adapter.java file
public class slidingmenuitem { string title; int icon; public slidingmenuitem(string title, int icon) { this.title = title; this.icon = icon; } public string gettitle() { return title; } public void settitle(string title) { this.title = title; } public int geticon() { return icon; } public void seticon(int icon) { this.icon = icon; } }
and menu item
<resources> <string name="app_name" translatable="false">fb slid</string> <string name="hello_world" translatable="false">hello world!</string> <string name="action_settings" translatable="false">settings</string> <string name="title_activity_main" translatable="false">mainactivity </string> <!-- sliding menu items --> <string-array name="nav_drawer_items"> <item >home</item> <item >notifications</item> <item >settings</item> <item >about</item> </string-array> <!-- sliding menu item icons --> <array name="nav_drawer_icons"> <item>@drawable/home</item> <item>@drawable/notifications</item> <item>@drawable/settings</item> <item>@drawable/about</item> </array> </resources>
and s string.xml file
log cat :
10-03 11:02:06.217 6956-6956/com.example.first.fbslid e/androidruntime﹕ fatal exception: main java.lang.runtimeexception: unable start activity componentinfo{com.example.first.fbslid/com.example.first.fbslid.mainactivity}: java.lang.arrayindexoutofboundsexception: length=0; index=0 @ android.app.activitythread.performlaunchactivity(activitythread.java:2121) @ android.app.activitythread.handlelaunchactivity(activitythread.java:2146) @ android.app.activitythread.access$700(activitythread.java:140) @ android.app.activitythread$h.handlemessage(activitythread.java:1238) @ android.os.handler.dispatchmessage(handler.java:99) @ android.os.looper.loop(looper.java:177) @ android.app.activitythread.main(activitythread.java:4947) @ java.lang.reflect.method.invokenative(native method) @ java.lang.reflect.method.invoke(method.java:511) @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:1038) @ com.android.internal.os.zygoteinit.main(zygoteinit.java:805) @ dalvik.system.nativestart.main(native method) caused by: java.lang.arrayindexoutofboundsexception: length=0; index=0 @ com.example.first.fbslid.mainactivity.oncreate(mainactivity.java:49) @ android.app.activity.performcreate(activity.java:5207) @ android.app.instrumentation.callactivityoncreate(instrumentation.java:1094) @ android.app.activitythread.performlaunchactivity(activitythread.java:2085) at android.app.activitythread.handlelaunchactivity(activitythread.java:2146) at android.app.activitythread.access$700(activitythread.java:140) at android.app.activitythread$h.handlemessage(activitythread.java:1238) at android.os.handler.dispatchmessage(handler.java:99) at android.os.looper.loop(looper.java:177) at android.app.activitythread.main(activitythread.java:4947) at java.lang.reflect.method.invokenative(native method) at java.lang.reflect.method.invoke(method.java:511) at com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:1038) at com.android.internal.os.zygoteinit.main(zygoteinit.java:805) at dalvik.system.nativestart.main(native method).
i don't know getting error new android , trying create sliding menu in facebook . can please me resolve
i think
<!-- sliding menu items -->
that empty, , when try read menu item array fails
titles = getresources().getstringarray(r.array.nav_drawer_items);
Comments
Post a Comment