android - Always Open Navigation-Drawer on Tablet -
i want keep navigation-drawer open in tablets. using following code when ever touch content frame getting null pointer exception.
my xml file:
<framelayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <android.support.v4.widget.drawerlayout android:id="@+id/drawer_layout" android:layout_width="match_parent" android:layout_height="match_parent"> <!-- navigation drawer --> <listview android:id="@+id/left_drawer" android:layout_width="@dimen/drawer_size" android:layout_height="match_parent" android:layout_gravity="start" android:choicemode="singlechoice" android:divider="@android:color/transparent" android:dividerheight="0dp" android:background="#111"/> </android.support.v4.widget.drawerlayout> <framelayout android:id="@+id/content_frame" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_marginleft="@dimen/drawer_content_padding"/> </framelayout>
mainactivity:
public class mainactivity extends fragmentactivity { private boolean isdrawerlocked = false; private listview drawerlist; private drawerlayout drawerlayout; private string[] draweritems; private actionbardrawertoggle drawertoggle; @override protected void oncreate(bundle bundle) { super.oncreate(bundle); setcontentview(r.layout.activity_main); drawerlist = (listview) findviewbyid(r.id.left_drawer); drawerlayout = (drawerlayout) findviewbyid(r.id.drawer_layout); framelayout framelayout = (framelayout)findviewbyid(r.id.content_frame); if(((viewgroup.marginlayoutparams)framelayout.getlayoutparams()).leftmargin == (int)getresources().getdimension(r.dimen.drawer_size)) { drawerlayout.setdrawerlockmode(drawerlayout.lock_mode_locked_open, drawerlist); drawerlayout.setscrimcolor(color.transparent); isdrawerlocked = true; } // set adapter list view draweritems = getresources().getstringarray(r.array.guide_elements); drawertoggle = new actionbardrawertoggle( this, drawerlayout, r.drawable.ic_drawer, r.string.app_name, r.string.app_name ) { /** called when drawer has settled in closed state. */ public void ondrawerclosed(view view) { getactionbar().settitle(gettitle()); invalidateoptionsmenu(); } /** called when drawer has settled in open state. */ public void ondraweropened(view drawerview) { getactionbar().settitle("select option"); invalidateoptionsmenu(); } }; if(!isdrawerlocked) { drawerlayout.setdrawerlistener(drawertoggle); } // set drawer toggle drawerlistener draweritemclicklistener draweritemclicklistener = new draweritemclicklistener(); drawerlist.setonitemclicklistener(draweritemclicklistener); if(!isdrawerlocked) { getactionbar().setdisplayhomeasupenabled(true); } } @override protected void onpostcreate(bundle savedinstancestate) { super.onpostcreate(savedinstancestate); drawertoggle.syncstate(); } @override public void onconfigurationchanged(configuration newconfig) { super.onconfigurationchanged(newconfig); drawertoggle.onconfigurationchanged(newconfig); } @override public boolean onoptionsitemselected(menuitem item) { // pass event actionbardrawertoggle, if returns // true, has handled app icon touch event if (drawertoggle.onoptionsitemselected(item)) { return true; } return super.onoptionsitemselected(item); } @override public boolean onprepareoptionsmenu(menu menu) { // if nav drawer open, hide action items related content view boolean draweropen = drawerlayout.isdraweropen(drawerlist); menuitem item = menu.finditem(r.id.action_settings); if(item != null) { item.setvisible(!draweropen); } item = menu.finditem(r.id.action_settings); if(item != null) { item.setvisible(!draweropen); } return super.onprepareoptionsmenu(menu); } /** * drawer item click listener */ private class draweritemclicklistener implements listview.onitemclicklistener { @override public void onitemclick(adapterview parent, view view, int position, long id) { selectitem(position); } /** swaps fragments in main content view */ private void selectitem(int position) { bundle bundle; switch(position) { } fragmentmanager fragmentmanager = getfragmentmanager(); fragmentmanager.begintransaction() .replace(r.id.content_frame, null) .commit(); // highlight selected item, update title, , close drawer drawerlist.setitemchecked(position, true); settitle(mainactivity.this.draweritems[position]); if(!isdrawerlocked) { drawerlayout.closedrawer(drawerlist); } } } }
but when ever touched content_frame getting null pointer exception. how can solve problem avoid null pointer exception.
exception details:
10-30 12:53:42.374: e/inputeventreceiver(3157): exception dispatching input event. 10-30 12:53:42.374: e/messagequeue-jni(3157): exception in messagequeue callback: handlereceivecallback 10-30 12:53:42.414: e/messagequeue-jni(3157): java.lang.nullpointerexception 10-30 12:53:42.414: e/messagequeue-jni(3157): @ android.support.v4.widget.drawerlayout.iscontentview(drawerlayout.java:853) 10-30 12:53:42.414: e/messagequeue-jni(3157): @ android.support.v4.widget.drawerlayout.onintercepttouchevent(drawerlayout.java:879) 10-30 12:53:42.414: e/messagequeue-jni(3157): @ android.view.viewgroup.dispatchtouchevent(viewgroup.java:1854) 10-30 12:53:42.414: e/messagequeue-jni(3157): @ android.view.viewgroup.dispatchtransformedtouchevent(viewgroup.java:2211) 10-30 12:53:42.414: e/messagequeue-jni(3157): @ android.view.viewgroup.dispatchtouchevent(viewgroup.java:1912) 10-30 12:53:42.414: e/messagequeue-jni(3157): @ android.view.viewgroup.dispatchtransformedtouchevent(viewgroup.java:2211) 10-30 12:53:42.414: e/messagequeue-jni(3157): @ android.view.viewgroup.dispatchtouchevent(viewgroup.java:1912) 10-30 12:53:42.414: e/messagequeue-jni(3157): @ android.view.viewgroup.dispatchtransformedtouchevent(viewgroup.java:2211) 10-30 12:53:42.414: e/messagequeue-jni(3157): @ android.view.viewgroup.dispatchtouchevent(viewgroup.java:1912) 10-30 12:53:42.414: e/messagequeue-jni(3157): @ android.view.viewgroup.dispatchtransformedtouchevent(viewgroup.java:2211) 10-30 12:53:42.414: e/messagequeue-jni(3157): @ android.view.viewgroup.dispatchtouchevent(viewgroup.java:1912) 10-30 12:53:42.414: e/messagequeue-jni(3157): @ com.android.internal.policy.impl.phonewindow$decorview.superdispatchtouchevent(phonewindow.java:1966) 10-30 12:53:42.414: e/messagequeue-jni(3157): @ com.android.internal.policy.impl.phonewindow.superdispatchtouchevent(phonewindow.java:1418) 10-30 12:53:42.414: e/messagequeue-jni(3157): @ android.app.activity.dispatchtouchevent(activity.java:2424) 10-30 12:53:42.414: e/messagequeue-jni(3157): @ com.android.internal.policy.impl.phonewindow$decorview.dispatchtouchevent(phonewindow.java:1914) 10-30 12:53:42.414: e/messagequeue-jni(3157): @ android.view.view.dispatchpointerevent(view.java:7564) 10-30 12:53:42.414: e/messagequeue-jni(3157): @ android.view.viewrootimpl$viewpostimeinputstage.processpointerevent(viewrootimpl.java:3883) 10-30 12:53:42.414: e/messagequeue-jni(3157): @ android.view.viewrootimpl$viewpostimeinputstage.onprocess(viewrootimpl.java:3778) 10-30 12:53:42.414: e/messagequeue-jni(3157): @ android.view.viewrootimpl$inputstage.deliver(viewrootimpl.java:3379) 10-30 12:53:42.414: e/messagequeue-jni(3157): @ android.view.viewrootimpl$inputstage.ondelivertonext(viewrootimpl.java:3429) 10-30 12:53:42.414: e/messagequeue-jni(3157): @ android.view.viewrootimpl$inputstage.forward(viewrootimpl.java:3398) 10-30 12:53:42.414: e/messagequeue-jni(3157): @ android.view.viewrootimpl$asyncinputstage.forward(viewrootimpl.java:3483) 10-30 12:53:42.414: e/messagequeue-jni(3157): @ android.view.viewrootimpl$inputstage.apply(viewrootimpl.java:3406) 10-30 12:53:42.414: e/messagequeue-jni(3157): @ android.view.viewrootimpl$asyncinputstage.apply(viewrootimpl.java:3540) 10-30 12:53:42.414: e/messagequeue-jni(3157): @ android.view.viewrootimpl$inputstage.deliver(viewrootimpl.java:3379) 10-30 12:53:42.414: e/messagequeue-jni(3157): @ android.view.viewrootimpl$inputstage.ondelivertonext(viewrootimpl.java:3429) 10-30 12:53:42.414: e/messagequeue-jni(3157): @ android.view.viewrootimpl$inputstage.forward(viewrootimpl.java:3398) 10-30 12:53:42.414: e/messagequeue-jni(3157): @ android.view.viewrootimpl$inputstage.apply(viewrootimpl.java:3406) 10-30 12:53:42.414: e/messagequeue-jni(3157): @ android.view.viewrootimpl$inputstage.deliver(viewrootimpl.java:3379) 10-30 12:53:42.414: e/messagequeue-jni(3157): @ android.view.viewrootimpl.deliverinputevent(viewrootimpl.java:5419) 10-30 12:53:42.414: e/messagequeue-jni(3157): @ android.view.viewrootimpl.doprocessinputevents(viewrootimpl.java:5399) 10-30 12:53:42.414: e/messagequeue-jni(3157): @ android.view.viewrootimpl.enqueueinputevent(viewrootimpl.java:5370) 10-30 12:53:42.414: e/messagequeue-jni(3157): @ android.view.viewrootimpl$windowinputeventreceiver.oninputevent(viewrootimpl.java:5493) 10-30 12:53:42.414: e/messagequeue-jni(3157): @ android.view.inputeventreceiver.dispatchinputevent(inputeventreceiver.java:182) 10-30 12:53:42.414: e/messagequeue-jni(3157): @ android.os.messagequeue.nativepollonce(native method) 10-30 12:53:42.414: e/messagequeue-jni(3157): @ android.os.messagequeue.next(messagequeue.java:132) 10-30 12:53:42.414: e/messagequeue-jni(3157): @ android.os.looper.loop(looper.java:124) 10-30 12:53:42.414: e/messagequeue-jni(3157): @ android.app.activitythread.main(activitythread.java:5103) 10-30 12:53:42.414: e/messagequeue-jni(3157): @ java.lang.reflect.method.invokenative(native method) 10-30 12:53:42.414: e/messagequeue-jni(3157): @ java.lang.reflect.method.invoke(method.java:525) 10-30 12:53:42.414: e/messagequeue-jni(3157): @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:737) 10-30 12:53:42.414: e/messagequeue-jni(3157): @ com.android.internal.os.zygoteinit.main(zygoteinit.java:553) 10-30 12:53:42.414: e/messagequeue-jni(3157): @ dalvik.system.nativestart.main(native method) 10-30 12:53:42.434: w/dalvikvm(3157): threadid=1: thread exiting uncaught exception (group=0x41465700) 10-30 12:53:42.484: e/androidruntime(3157): fatal exception: main 10-30 12:53:42.484: e/androidruntime(3157): java.lang.nullpointerexception 10-30 12:53:42.484: e/androidruntime(3157): @ android.support.v4.widget.drawerlayout.iscontentview(drawerlayout.java:853) 10-30 12:53:42.484: e/androidruntime(3157): @ android.support.v4.widget.drawerlayout.onintercepttouchevent(drawerlayout.java:879) 10-30 12:53:42.484: e/androidruntime(3157): @ android.view.viewgroup.dispatchtouchevent(viewgroup.java:1854) 10-30 12:53:42.484: e/androidruntime(3157): @ android.view.viewgroup.dispatchtransformedtouchevent(viewgroup.java:2211) 10-30 12:53:42.484: e/androidruntime(3157): @ android.view.viewgroup.dispatchtouchevent(viewgroup.java:1912) 10-30 12:53:42.484: e/androidruntime(3157): @ android.view.viewgroup.dispatchtransformedtouchevent(viewgroup.java:2211) 10-30 12:53:42.484: e/androidruntime(3157): @ android.view.viewgroup.dispatchtouchevent(viewgroup.java:1912) 10-30 12:53:42.484: e/androidruntime(3157): @ android.view.viewgroup.dispatchtransformedtouchevent(viewgroup.java:2211) 10-30 12:53:42.484: e/androidruntime(3157): @ android.view.viewgroup.dispatchtouchevent(viewgroup.java:1912) 10-30 12:53:42.484: e/androidruntime(3157): @ android.view.viewgroup.dispatchtransformedtouchevent(viewgroup.java:2211) 10-30 12:53:42.484: e/androidruntime(3157): @ android.view.viewgroup.dispatchtouchevent(viewgroup.java:1912) 10-30 12:53:42.484: e/androidruntime(3157): @ com.android.internal.policy.impl.phonewindow$decorview.superdispatchtouchevent(phonewindow.java:1966) 10-30 12:53:42.484: e/androidruntime(3157): @ com.android.internal.policy.impl.phonewindow.superdispatchtouchevent(phonewindow.java:1418) 10-30 12:53:42.484: e/androidruntime(3157): @ android.app.activity.dispatchtouchevent(activity.java:2424) 10-30 12:53:42.484: e/androidruntime(3157): @ com.android.internal.policy.impl.phonewindow$decorview.dispatchtouchevent(phonewindow.java:1914) 10-30 12:53:42.484: e/androidruntime(3157): @ android.view.view.dispatchpointerevent(view.java:7564) 10-30 12:53:42.484: e/androidruntime(3157): @ android.view.viewrootimpl$viewpostimeinputstage.processpointerevent(viewrootimpl.java:3883) 10-30 12:53:42.484: e/androidruntime(3157): @ android.view.viewrootimpl$viewpostimeinputstage.onprocess(viewrootimpl.java:3778) 10-30 12:53:42.484: e/androidruntime(3157): @ android.view.viewrootimpl$inputstage.deliver(viewrootimpl.java:3379) 10-30 12:53:42.484: e/androidruntime(3157): @ android.view.viewrootimpl$inputstage.ondelivertonext(viewrootimpl.java:3429) 10-30 12:53:42.484: e/androidruntime(3157): @ android.view.viewrootimpl$inputstage.forward(viewrootimpl.java:3398) 10-30 12:53:42.484: e/androidruntime(3157): @ android.view.viewrootimpl$asyncinputstage.forward(viewrootimpl.java:3483) 10-30 12:53:42.484: e/androidruntime(3157): @ android.view.viewrootimpl$inputstage.apply(viewrootimpl.java:3406) 10-30 12:53:42.484: e/androidruntime(3157): @ android.view.viewrootimpl$asyncinputstage.apply(viewrootimpl.java:3540) 10-30 12:53:42.484: e/androidruntime(3157): @ android.view.viewrootimpl$inputstage.deliver(viewrootimpl.java:3379) 10-30 12:53:42.484: e/androidruntime(3157): @ android.view.viewrootimpl$inputstage.ondelivertonext(viewrootimpl.java:3429) 10-30 12:53:42.484: e/androidruntime(3157): @ android.view.viewrootimpl$inputstage.forward(viewrootimpl.java:3398) 10-30 12:53:42.484: e/androidruntime(3157): @ android.view.viewrootimpl$inputstage.apply(viewrootimpl.java:3406) 10-30 12:53:42.484: e/androidruntime(3157): @ android.view.viewrootimpl$inputstage.deliver(viewrootimpl.java:3379) 10-30 12:53:42.484: e/androidruntime(3157): @ android.view.viewrootimpl.deliverinputevent(viewrootimpl.java:5419) 10-30 12:53:42.484: e/androidruntime(3157): @ android.view.viewrootimpl.doprocessinputevents(viewrootimpl.java:5399) 10-30 12:53:42.484: e/androidruntime(3157): @ android.view.viewrootimpl.enqueueinputevent(viewrootimpl.java:5370) 10-30 12:53:42.484: e/androidruntime(3157): @ android.view.viewrootimpl$windowinputeventreceiver.oninputevent(viewrootimpl.java:5493) 10-30 12:53:42.484: e/androidruntime(3157): @ android.view.inputeventreceiver.dispatchinputevent(inputeventreceiver.java:182) 10-30 12:53:42.484: e/androidruntime(3157): @ android.os.messagequeue.nativepollonce(native method) 10-30 12:53:42.484: e/androidruntime(3157): @ android.os.messagequeue.next(messagequeue.java:132) 10-30 12:53:42.484: e/androidruntime(3157): @ android.os.looper.loop(looper.java:124) 10-30 12:53:42.484: e/androidruntime(3157): @ android.app.activitythread.main(activitythread.java:5103) 10-30 12:53:42.484: e/androidruntime(3157): @ java.lang.reflect.method.invokenative(native method) 10-30 12:53:42.484: e/androidruntime(3157): @ java.lang.reflect.method.invoke(method.java:525) 10-30 12:53:42.484: e/androidruntime(3157): @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:737) 10-30 12:53:42.484: e/androidruntime(3157): @ com.android.internal.os.zygoteinit.main(zygoteinit.java:553) 10-30 12:53:42.484: e/androidruntime(3157): @ dalvik.system.nativestart.main(native method)
how can solve problem avoid null pointer exception
i think bcoz in naivagtion drawer.
there must 2 layouts.
the 1st layout in backend means when drawer opens view appears and
the 2nd layout front view i.e when drawer not opened.
so,
try adding second layout in drawer layout below listview.
for e.g.
<android.support.v4.widget.drawerlayout android:id="@+id/drawer_layout" android:layout_width="match_parent" android:layout_height="match_parent"> <!-- navigation drawer --> <listview android:id="@+id/left_drawer" android:layout_width="@dimen/drawer_size" android:layout_height="match_parent" android:layout_gravity="start" android:choicemode="singlechoice" android:divider="@android:color/transparent" android:dividerheight="0dp" android:background="#111"/> <textview android:id="@+id/textview1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="welcome" android:textappearance="?android:attr/textappearancelarge" android:textcolor="#090" /> </android.support.v4.widget.drawerlayout>
Comments
Post a Comment