Sunday 15 September 2013

java - Navigation Drawer Error Android -



java - Navigation Drawer Error Android -

hello have 1 navigation drawer , when open swipe ---> run god, when touch in action bar app broken

my 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" android:background="#ffffff" > <!-- framelayout display fragments --> <framelayout android:id="@+id/content_frame" android:layout_width="match_parent" android:layout_height="match_parent" /> <!-- listview display slider menu --> <linearlayout android:id="@+id/left_drawer_s" android:layout_width="240dp" android:layout_height="match_parent" android:layout_gravity="start" android:orientation="vertical" > <linearlayout android:id="@+id/drawers" android:layout_width="240dp" android:layout_height="wrap_content" android:layout_gravity="start" android:background="@color/redar_color" android:gravity="center_vertical" android:orientation="horizontal" > <imageview android:layout_width="80dp" android:layout_height="80dp" android:paddingleft="20dp" android:src="@drawable/ic_launcher" /> <linearlayout android:id="@+id/drawer" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginleft="10dp" android:background="@color/redar_color" android:orientation="vertical" > <textview android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center" android:text="shohel rana" android:textsize="14sp" /> <textview android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margintop="5dp" android:gravity="center" android:text="test@gmail.com" android:textsize="14sp" /> </linearlayout> </linearlayout> <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="#fff"/> </linearlayout> </android.support.v4.widget.drawerlayout>

and class in java :

private drawerlayout mdrawerlayout; private listview mdrawerlist; private actionbardrawertoggle mdrawertoggle; private charsequence mdrawertitle; private charsequence mtitle; private string[] nav_options; private int restaurant_user_id = 0; int position= 0; public static drawer_menu drawer_menu_activity; protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.drawer_menu); drawer_menu_activity = this; getactionbar().setdisplayhomeasupenabled(true); getactionbar().sethomebuttonenabled(true); nav_options = getresources().getstringarray(r.array.nav_options); mdrawerlayout = (drawerlayout) findviewbyid(r.id.drawer_layout); mdrawerlist = (listview) findviewbyid(r.id.left_drawer); // set adapter list view arrayadapter<string> adapter = new arrayadapter<string>(this,android.r.layout.simple_list_item_1, nav_options); mdrawerlist.setadapter(adapter); // set list's click listener mdrawerlist.setonitemclicklistener(new draweritemclicklistener()); position = 1; selectitem(position,restaurant_user_id); mdrawertoggle = new actionbardrawertoggle( this, /* host activity */ mdrawerlayout, /* drawerlayout object */ r.drawable.ic_drawer, /* nav drawer icon replace 'up' caret */ r.string.hello_world, /* "open drawer" description */ r.string.hello_world /* "close drawer" description */ ) { /** called when drawer has settled in closed state. */ public void ondrawerclosed(view view) { super.ondrawerclosed(view); invalidateoptionsmenu(); } /** called when drawer has settled in open state. */ public void ondraweropened(view drawerview) { super.ondraweropened(drawerview); invalidateoptionsmenu(); } }; } @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); } /* click listner listview in navigation drawer */ private class draweritemclicklistener implements listview.onitemclicklistener { @override public void onitemclick(adapterview<?> parent, view view, int position, long id) { selectitem(position,restaurant_user_id); } } private void selectitem(int position, int restaurant_user_id) { mdrawerlayout.closedrawers(); bundle args = new bundle(); args.putint("restaurant_user_id", restaurant_user_id); args.putstring("web_url", web_url); switch (position){ case 0: log.v("position" , "es 0"); position=0; fragment main= new main(); main.setarguments(args); fragmentmanager fragmentmanager_main = getfragmentmanager(); fragmentmanager_main.begintransaction().replace(r.id.content_frame, main).commit(); break; case 1: log.v("position" , "es 1"); position=1; fragment detail = new comments(); detail.setarguments(args); fragmentmanager fragmentmanager = getfragmentmanager(); fragmentmanager.begintransaction().replace(r.id.content_frame, detail).commit(); break; } } @override public boolean onoptionsitemselected(menuitem item) { switch (item.getitemid()) { case android.r.id.home: mdrawerlayout.opendrawer(mdrawerlist); mdrawerlayout.invalidate(); homecoming true; default: homecoming super.onoptionsitemselected(item); } }

log console

11-11 09:42:00.414 10471-10471/mx.clai.redar.redarmanager e/androidruntime﹕ fatal exception: main java.lang.classcastexception: android.widget.linearlayout$layoutparams cannot cast android.support.v4.widget.drawerlayout$layoutparams @ android.support.v4.widget.drawerlayout.isdrawerview(drawerlayout.java:968) @ android.support.v4.widget.drawerlayout.opendrawer(drawerlayout.java:1129) @ mx.clai.redar.redarmanager.drawer_menu.onoptionsitemselected(drawer_menu.java:204) @ android.app.activity.onmenuitemselected(activity.java:2610) @ com.android.internal.widget.actionbarview$3.onclick(actionbarview.java:243) @ android.view.view.performclick(view.java:4421) @ android.view.view$performclick.run(view.java:18190) @ android.os.handler.handlecallback(handler.java:725) @ android.os.handler.dispatchmessage(handler.java:92) @ android.os.looper.loop(looper.java:175) @ android.app.activitythread.main(activitythread.java:5279) @ java.lang.reflect.method.invokenative(native method) @ java.lang.reflect.method.invoke(method.java:511) @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:1102) @ com.android.internal.os.zygoteinit.main(zygoteinit.java:869) @ dalvik.system.nativestart.main(native method)

console market drawer_menu.java:204 a:

`mdrawerlayout.opendrawer(mdrawerlist);`

please help , regards!

try alter onoptionsitemselected method:

@override public boolean onoptionsitemselected(menuitem item) { // pass event actionbardrawertoggle, if returns // true, has handled app icon touch event if (mdrawertoggle.onoptionsitemselected(item)) { homecoming true; } // handle other action bar items... homecoming super.onoptionsitemselected(item); }

that should work.

java android xml navigation-drawer

No comments:

Post a Comment