Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

InflateException: Couldn't resolve menu item onClick handler

I asked this question 6 years ago. In the meantime Android development best practices have changed, and I have become a better developer.

Since then, I have realized that using the onClick XML attribute is a bad practice, and have removed it from any code base I work on.

All of my click handlers are now defined in the code of the app, not the XML layouts!

My reasons for never using onClick are

  1. it is easy to make a mistake in the value of the onClick XML attribute, which will then result in a run-time error
  2. a developer might refactor the name of the click handler method, without realizing it is called from a layout (see reason 1)
  3. finding out which method is actually being called is not always obvious. Especially if the layout is being used by a Fragment
  4. separating the concerns of layout vs behavior is good. Using onClick mixes them up, which is bad!

I hope I have convinced you to never use onClick in a layout :) !

Below is my original question, which is a pretty good illustration of why using onClick is a bad idea.

===

I'm defining menu items in XML, and trying to use the onClick attribute that was added in API 11. When the Activity is launched in an emulator running 4.0.3, the following Exceptions occur:

FATAL EXCEPTION: main android.view.InflateException: Couldn't resolve menu item onClick handler      onFeedbackMenu in class android.view.ContextThemeWrapper  ... Caused by: java.lang.NoSuchMethodException: onFeedbackMenu      [interface com.actionbarsherlock.view.MenuItem] at java.lang.Class.getConstructorOrMethod(Class.java:460) 

I don't understand what is causing the Exception, since the following method is defined in my Activity

import com.actionbarsherlock.view.MenuItem; ... public void onFeedbackMenu( MenuItem menuItem ) {      Toast.makeText( this, "onFeedBack", Toast.LENGTH_LONG ).show(); } 

My XML menu definition file contains:

<menu xmlns:android="http://schemas.android.com/apk/res/android" > ...     <item         android:id="@+id/menu_feedback"         android:icon="@drawable/ic_action_share"         android:showAsAction="ifRoom"         android:title="@string/menu_feedback"         android:onClick="onFeedbackMenu" /> </menu> 

For backwards compatibility I am using ActionBarSherlock, and also getting a very similar Exception when I run the App on 2.3.x.

This is a more Complete version of the Stack trace

FATAL EXCEPTION: main android.view.InflateException: Couldn't resolve menu item onClick handler      onFeedbackMenu in class android.view.ContextThemeWrapper     at com.actionbarsherlock.view.MenuInflater$InflatedOnMenuItemClickListener.<init>(MenuInflater.java:204)     at com.actionbarsherlock.view.MenuInflater$MenuState.setItem(MenuInflater.java:410)     at com.actionbarsherlock.view.MenuInflater$MenuState.addItem(MenuInflater.java:445)     at com.actionbarsherlock.view.MenuInflater.parseMenu(MenuInflater.java:175)     at com.actionbarsherlock.view.MenuInflater.inflate(MenuInflater.java:97)     ... Caused by: java.lang.NoSuchMethodException: onFeedbackMenu      [interface com.actionbarsherlock.view.MenuItem]     at java.lang.Class.getConstructorOrMethod(Class.java:460)     at java.lang.Class.getMethod(Class.java:915)     at com.actionbarsherlock.view.MenuInflater$InflatedOnMenuItemClickListener.<init>(MenuInflater.java:202)     ... 23 more 
like image 351
Frank Harper Avatar asked Jun 28 '12 13:06

Frank Harper


1 Answers

I found a solution that worked for me. Usually the onClick attribute in a layout has the following method

public void methodname(View view) {      // actions } 

On a menu item (in this case Sherlock menu) it should follow the following signature:

public boolean methodname(MenuItem item) {      // actions } 

So, your problem was that your method returned void and not boolean.

like image 88
Milagaia Avatar answered Sep 18 '22 13:09

Milagaia