android - NavigationView onClick from xml causes errors -
using navigationview newly released android support design library, if navigation header layout includes onclick (in xml), onclick event crashes app. onclick can added programmatically via view.onclicklistener (instead of xml), , clicking works fine. reason, whenever xml onclick used, there error.
here's main layout:
<android.support.v4.widget.drawerlayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:id="@+id/mainactivitylayout" android:layout_width="match_parent" android:layout_height="match_parent" android:fitssystemwindows="true"> <relativelayout android:id="@+id/maincontentframe" android:layout_width="match_parent" android:layout_height="match_parent"> ... </relativelayout> <android.support.design.widget.navigationview android:id="@+id/drawernavview" android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_gravity="start" android:fitssystemwindows="true" app:headerlayout="@layout/drawer_header" app:menu="@menu/drawer_menu"> </android.support.design.widget.navigationview> </android.support.v4.widget.drawerlayout> in activity, menu item clicks (added navview.setnavigationitemselectedlistener()) work fine. problem when header clicked:
drawer_header.xml:
... <view android:id="@+id/testbutton" android:layout_width="match_parent" android:layout_height="60dp" android:onclick="testbuttonclick"/> ... produces following error:
java.lang.illegalstateexception: not find method testbuttonclick(view) in activity class android.view.contextthemewrapper onclick handler on view class android.view.view id 'testbutton' update
navigationview can use standard menu resource files, there similar problem if using onclick menu xml resource. according menu resource reference, android:onclick attribute overrides normal callbacks. works fine, menu items in navigationview, doesn't. instead, crashes error:
java.lang.runtimeexception: unable start activity componentinfo{...}: android.view.inflateexception: binary xml file line #34: error inflating class android.support.design.widget.navigationview the error goes away when remove xml onclick.
update
i tested xml onclick using "official" demo project android design library. same results: adding onclick (in xml) navigationview's menu or header causes app crash. appears bug navigationview.
resolved in v23.1
google released a fix these xml onclick errors in support library v23.1.
confirmed, bug in support library.
apparently it's related contextthemewrapper, , according bug report, problem exists in support library 22.1.
so, short answer is:
don't use xml onclick navigationview (or other components edittext), until it's fixed.
workaround:
set click listeners in code. navigationview, use setnavigationitemselectedlistener().
update: bug has ben fixed
you can use xml onclick in support library 23.1 (bug report). i've verified works in app. there seems other (newer) xml issues navview in v23.1 (see below), though particular onclick error fixed.
for completeness:
there appears (related?) bug when inflating navigationview header via xml. using xml app:headerlayout produces errors 23.1, though xml onclick works. because of inflation issue, you'll need use navigationview.inflateheaderview() method in code. new method added in 23.1 and, apparently, previous xml inflate broken (or maybe deprecated app:headerlayout without telling anyone?). more info detailed here.
Comments
Post a Comment