In the headerLayout
of a NavigationView
(on the left side of a DrawerLayout
app) I have an ImageButton
, which should represent ability to add a social network account:
Here is the activity_main.xml with the NavigationView
:
<android.support.design.widget.NavigationView
android:id="@+id/left_drawer"
android:layout_height="match_parent"
android:layout_width="wrap_content"
android:layout_gravity="start"
android:fitsSystemWindows="true"
android:background="#FFF"
app:itemIconTint="@color/drawer_item_text"
app:itemTextColor="@color/drawer_item_text"
app:headerLayout="@layout/header_left"
app:menu="@menu/drawer_left"/>
And here the header_left.xml with the ImageButton
:
<ImageButton
android:id="@+id/account"
android:src="@drawable/ic_account_plus_white_24dp"
android:background="@color/drawer_header_bg"
android:onClick="showAccounts"
android:padding="0dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right|bottom"
/>
In the MainActivity.java I have defined the click handler method:
public void showAccounts(View v) {
Log.d(TAG, "accounts button clicked");
}
Unfortunately, the app crashes on the ImageButton
click with:
java.lang.IllegalStateException: Could not find a method showAccounts(View) in the activity class android.view.ContextThemeWrapper for onClick handler on view class android.widget.ImageButton with id 'account'
at android.view.View$1.onClick(View.java:4013)
at android.view.View.performClick(View.java:4785)
at android.view.View$PerformClick.run(View.java:19858)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:155)
at android.app.ActivityThread.main(ActivityThread.java:5696)
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:1028)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:823)
Caused by: java.lang.NoSuchMethodException: showAccounts [class android.view.View]
at java.lang.Class.getMethod(Class.java:664)
at java.lang.Class.getMethod(Class.java:643)
at android.view.View$1.onClick(View.java:4006)
at android.view.View.performClick(View.java:4785)
at android.view.View$PerformClick.run(View.java:19858)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:155)
at android.app.ActivityThread.main(ActivityThread.java:5696)
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:1028)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:823)
Why does it happen and how to work around the problem please? (Besides adding onClickListener
in MainActivity.java).
I have another ImageButton
in the Toolbar
of the same app - and its onclick handler is called in MainActivity.java just fine.
I had the same problem, and I fixed it with the next way:
first remove the attribute app:headerLayout from the android.support.design.widget.NavigationView, because we need add this programmatically.
Now in your class when use the NavigationView do something like this:
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
headerView = navigationView.inflateHeaderView(R.layout.header_left);
ImageButton accountButton = (ImageButton) headerView.findViewById(R.id.account);
accountButton.setOnClickListener...