Issue
Using NavigationView from the newly released Android support design library, if a navigation header layout includes an onClick (in the xml), onClick event crashes app. OnClick can be added programmatically via view.onClickListener
(instead of xml), and then clicking works fine. But for some reason, whenever xml onClick is used, there is an error.
Here's my 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 my activity, my menu item clicks (added with navView.setNavigationItemSelectedListener()
) work fine. The problem is when the header is clicked:
drawer_header.xml:
...
<View
android:id="@+id/testButton"
android:layout_width="match_parent"
android:layout_height="60dp"
android:onClick="testButtonClick"/>
...
Produces the following error:
java.lang.IllegalStateException: Could not find a method testButtonClick(View)
in the activity class android.view.ContextThemeWrapper for onClick handler
on view class android.view.View with id 'testButton'
UPDATE
NavigationView can use standard Menu resource files, but there is a similar problem if using onClick from the menu XML resource. According to the Menu Resource reference, the android:onClick
attribute overrides the normal callbacks. This usually works fine, but with menu items in NavigationView, it doesn't. Instead, it crashes with this error:
java.lang.RuntimeException: Unable to start activity ComponentInfo{...}:
android.view.InflateException: Binary XML file line #34:
Error inflating class android.support.design.widget.NavigationView
The error goes away when I remove the XML onClick.
UPDATE
I tested xml onClick using the "official" demo project for the Android Design Library. Same results: adding onClick (in xml) to a NavigationView's menu or header causes the app the crash. So this appears to be a bug with NavigationView.
RESOLVED IN v23.1
Google released a fix for these XML onClick errors in Support Library v23.1.
Solution
Confirmed, this is a bug in the support library.
Apparently it's related to ContextThemeWrapper
, and according to this bug report, the problem exists in Support Library 22.1.
So, the short answer is:
Don't use XML onClick with NavigationView (or some other components like EditText), until it's fixed.
Workaround:
Set click listeners in code. For NavigationView, use setNavigationItemSelectedListener()
.
UPDATE: This bug has ben fixed
You can now use XML onClick in Support Library 23.1 (bug report). I've verified it works in my app. But there seems to be other (newer) XML issues with NavView in v23.1 (see below), even though this particular onClick error is now fixed.
For completeness:
There appears to be another (related?) bug when inflating NavigationView header via XML. Using XML app:headerLayout
produces errors with 23.1, even though XML onClick now works. Because of this inflation issue, you'll need to use NavigationView.inflateHeaderView()
method in code. This new method was added in 23.1 and, apparently, the previous XML inflate is now broken (or maybe they deprecated app:headerLayout
without telling anyone?). More info detailed here.
Answered By - hungryghost
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.