android - NullPointerException when initializing NavigationDrawer -
i'm trying use navagation drawer
in application , keep running nullpointerexceptions , don't understand why. current code based largely off of this developer link is:
public class mainactivity extends activity { private string[] mpages; private drawerlayout mdrawerlayout; private listview mdrawerlist; private actionbardrawertoggle mdrawertoggle; private charsequence mdrawertitle; private charsequence mtitle; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); mpages = getresources().getstringarray(r.array.page_titles); mdrawerlayout = (drawerlayout) findviewbyid(r.id.drawer_layout); mdrawerlist = (listview) findviewbyid(r.id.left_drawer); mdrawerlist.setadapter(new arrayadapter<string>(this, r.layout.drawer_list_item, mpages)); mtitle = mdrawertitle = gettitle(); mdrawerlayout = (drawerlayout) findviewbyid(r.id.drawer_layout); mdrawertoggle = new actionbardrawertoggle(this, mdrawerlayout, r.drawable.ic_drawer, r.string.drawer_open, r.string.drawer_close ) { public void ondrawerclosed(view view) { getactionbar().settitle(mtitle); } public void ondraweropened(view drawerview) { getactionbar().settitle(mdrawertitle); } }; mdrawerlayout.setdrawerlistener(mdrawertoggle); getactionbar().setdisplayhomeasupenabled(true); getactionbar().sethomebuttonenabled(true); } @override protected void onpostcreate(bundle savedinstancestate) { super.onpostcreate(savedinstancestate); // sync toggle state after onrestoreinstancestate has occurred. mdrawertoggle.syncstate(); } @override public void onconfigurationchanged(configuration newconfig) { super.onconfigurationchanged(newconfig); mdrawertoggle.onconfigurationchanged(newconfig); } @override public boolean onoptionsitemselected(menuitem item) { if (mdrawertoggle.onoptionsitemselected(item)) { return true; } return super.onoptionsitemselected(item); } @override public boolean oncreateoptionsmenu(menu menu) { getmenuinflater().inflate(r.menu.main, menu); return true; } }
and drawer_layout.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"> <!-- main content view --> <framelayout android:id="@+id/content_frame" android:layout_width="match_parent" android:layout_height="match_parent" /> <!-- navigation drawer --> <listview android:id="@+id/left_drawer" android:layout_width="240dp" android:layout_height="match_parent" android:layout_gravity="start" android:choicemode="singlechoice" android:divider="@android:color/transparent" android:dividerheight="0dp" android:background="#111"/>
my stacktrace:
08-12 15:55:54.524: e/androidruntime(1409): fatal exception: main 08-12 15:55:54.524: e/androidruntime(1409): java.lang.runtimeexception: unable start activity componentinfo{com.test.android.nav/com.test.android.nav.mainactivity}: java.lang.nullpointerexception 08-12 15:55:54.524: e/androidruntime(1409): @ android.app.activitythread.performlaunchactivity(activitythread.java:2059) 08-12 15:55:54.524: e/androidruntime(1409): @ android.app.activitythread.handlelaunchactivity(activitythread.java:2084) 08-12 15:55:54.524: e/androidruntime(1409): @ android.app.activitythread.access$600(activitythread.java:130) 08-12 15:55:54.524: e/androidruntime(1409): @ android.app.activitythread$h.handlemessage(activitythread.java:1195) 08-12 15:55:54.524: e/androidruntime(1409): @ android.os.handler.dispatchmessage(handler.java:99) 08-12 15:55:54.524: e/androidruntime(1409): @ android.os.looper.loop(looper.java:137) 08-12 15:55:54.524: e/androidruntime(1409): @ android.app.activitythread.main(activitythread.java:4745) 08-12 15:55:54.524: e/androidruntime(1409): @ java.lang.reflect.method.invokenative(native method) 08-12 15:55:54.524: e/androidruntime(1409): @ java.lang.reflect.method.invoke(method.java:511) 08-12 15:55:54.524: e/androidruntime(1409): @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:786) 08-12 15:55:54.524: e/androidruntime(1409): @ com.android.internal.os.zygoteinit.main(zygoteinit.java:553) 08-12 15:55:54.524: e/androidruntime(1409): @ dalvik.system.nativestart.main(native method) 08-12 15:55:54.524: e/androidruntime(1409): caused by: java.lang.nullpointerexception 08-12 15:55:54.524: e/androidruntime(1409): @ com.test.android.nav.mainactivity.oncreate(mainactivity.java:30) 08-12 15:55:54.524: e/androidruntime(1409): @ android.app.activity.performcreate(activity.java:5008) 08-12 15:55:54.524: e/androidruntime(1409): @ android.app.instrumentation.callactivityoncreate(instrumentation.java:1079) 08-12 15:55:54.524: e/androidruntime(1409): @ android.app.activitythread.performlaunchactivity(activitythread.java:2023) 08-12 15:55:54.524: e/androidruntime(1409): ... 11 more
line 30 : mdrawerlist.setadapter(new arrayadapter<string>(this, r.layout.drawer_list_item, mpages));
, have xml file in res folder.
now, before put in line, getting nullpointerexception
on line: mdrawerlayout.setdrawerlistener(mdrawertoggle);
any appreciated!
i see 2 potential reasons nullpointerexception:
the first 1 line:
mpages = getresources().getstringarray(r.array.page_titles);
does not return valid array mpages. suppose you not have string array called "page_titles" in resources folder.
if posted code absolutely correct, 1 other reason line:
setcontentview(r.layout.activity_main);
since according code, layoutfile of navigationdrawer drawer_layout.xml. should this:
setcontentview(r.layout.drawer_layout);
in opinion, resource id of drawerlayout , name of .xml file both being "drawer_layout" misleading, recommend change that.
furthermore, don't see reason why calling line:
mdrawerlayout = (drawerlayout) findviewbyid(r.id.drawer_layout);
twice in oncreate() method.
Comments
Post a Comment