java - Android DrawLayout Implementation -
ive been following post
android navigation drawer implemented activities
however application crashes before displays content. doesnt appear the oncreate menthod not triggering breakpoints
here mainactivity
package com.example.alex.menutest2; import android.content.intent; import android.support.v4.widget.drawerlayout; import android.support.v7.app.actionbaractivity; import android.os.bundle; import android.view.menu; import android.view.menuitem; import android.view.view; import android.widget.adapterview; import android.widget.listview; public class mainactivity extends actionbaractivity { public listview mleftdrawerlist = (listview)findviewbyid(r.id.left_drawer); public drawerlayout mdrawerlayout = (drawerlayout) findviewbyid(r.id.drawer_layout); protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); // other stuff initialize drawer layout, add list items // ……… // ………. // add listener drawer list view mleftdrawerlist.setonitemclicklistener(new draweritemclicklistener()); } private class draweritemclicklistener implements listview.onitemclicklistener { @override public void onitemclick(adapterview<?> adapterview, view view, int position, long id) { switch (position) { case 0: { intent intent = new intent(mainactivity.this, documentactivity.class); startactivity(intent); break; } default: break; } mdrawerlayout.closedrawer(mleftdrawerlist); } } }
here xml layout
<android.support.v4.widget.drawerlayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_height="match_parent" android:layout_width="match_parent" android:id="@+id/drawer_layout"> <framelayout android:id="@+id/activity_frame"/> <listview android:id="@+id/left_drawer" android:entries="@array/menu_array"/> </android.support.v4.widget.drawerlayout>
here stack trace
06-14 19:14:26.127 8093-8093/com.example.alex.menutest2 e/androidruntime﹕ fatal exception: main process: com.example.alex.menutest2, pid: 8093 java.lang.runtimeexception: unable instantiate activity componentinfo{com.example.alex.menutest2/com.example.alex.menutest2.mainactivity}: java.lang.nullpointerexception: attempt invoke virtual method 'android.view.view android.view.window.findviewbyid(int)' on null object reference @ android.app.activitythread.performlaunchactivity(activitythread.java:2236) @ android.app.activitythread.handlelaunchactivity(activitythread.java:2390) @ android.app.activitythread.access$800(activitythread.java:151) @ android.app.activitythread$h.handlemessage(activitythread.java:1303) @ android.os.handler.dispatchmessage(handler.java:102) @ android.os.looper.loop(looper.java:135) @ android.app.activitythread.main(activitythread.java:5257) @ java.lang.reflect.method.invoke(native method) @ java.lang.reflect.method.invoke(method.java:372) @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:903) @ com.android.internal.os.zygoteinit.main(zygoteinit.java:698) caused by: java.lang.nullpointerexception: attempt invoke virtual method 'android.view.view android.view.window.findviewbyid(int)' on null object reference @ android.app.activity.findviewbyid(activity.java:2072) @ com.example.alex.menutest2.mainactivity.<init>(mainactivity.java:17) @ java.lang.reflect.constructor.newinstance(native method) @ java.lang.class.newinstance(class.java:1606) @ android.app.instrumentation.newactivity(instrumentation.java:1066) @ android.app.activitythread.performlaunchactivity(activitythread.java:2226) at android.app.activitythread.handlelaunchactivity(activitythread.java:2390) at android.app.activitythread.access$800(activitythread.java:151) at android.app.activitythread$h.handlemessage(activitythread.java:1303) at android.os.handler.dispatchmessage(handler.java:102) at android.os.looper.loop(looper.java:135) at android.app.activitythread.main(activitythread.java:5257) at java.lang.reflect.method.invoke(native method) at java.lang.reflect.method.invoke(method.java:372) at com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:903) at com.android.internal.os.zygoteinit.main(zygoteinit.java:698)
i might wrong, think shouldn't initialize global parameters before oncreate() method called. you're trying initialize them findviewbyid()
before contentview set.
based on error log, you've got nullpointer @ mdrawerlayout, makes me guess following line didn't find view:
public drawerlayout mdrawerlayout = (drawerlayout) findviewbyid(r.id.drawer_layout);
Comments
Post a Comment