-2

So, to be more specific, OnClick is working just fine if I'm using a view item (Such as a button). As far as menu items go though, it's failing completely.

Here's my main.xml from the menu folder:

<menu xmlns:android="http://schemas.android.com/apk/res/android" >

<item
    android:id="@+id/action_settings"
    android:orderInCategory="100"
    android:showAsAction="never"
    android:title="@string/action_settings" 
    android:onClick="openSettings" />
<item
    android:id="@+id/action_high_scores"
    android:orderInCategory="100"
    android:showAsAction="always"
    android:title="@string/action_high_scores" />

And here's the relevant code from MainActivity.java:

    public void openSettings(MenuItem item) {
    Intent openSettings = new Intent(getApplicationContext(), Settings.class);
    startActivity(openSettings);
}

I've placed the openSettings method before and after the onCreateOptionsMenu() method declaration but it made no difference.

Every time, it returns the same error: NoSuchMethodException Ideas? Thanks in advance.

(UPDATE: I'm on API 15 so no, it's NOT an API compatibility issue. Using onClick in the Menu list works 100% with API's 11 and greater, so please don't tell me to use the onMenuItemSelected method.)

UPDATE: So I plugged in my tablet (API 11, Honeycomb) and POOF! It works! For some reason or another, API 15, 4.0.4 ISC isn't working with this!?!?! Any ideas what would be causing it? (In the meantime, for compatability, it looks like I may have to override onMenuItemSelected after all, despite my wishes otherwise. Did I just hit a major bug with API 15?

JRad the Bad
  • 511
  • 5
  • 25
  • bro, this is as relevant as the code gets. everything else is literally android API... :/ (Apparently you aren't familiar with how android works with XML layouts?) – JRad the Bad Feb 21 '14 at 16:25
  • only in the event of view items. the XML is for the menu and must accept the MenuItem as a parameter. "The method must be public and accept a single MenuItem parameter" -(From http://developer.android.com/guide/topics/ui/menus.html) – JRad the Bad Feb 21 '14 at 16:44
  • i just saw that now. but i suggest you rename your intent coz it has the same name with the method name – Raghunandan Feb 21 '14 at 16:44
  • well i tried the same it works . i made a sample to test – Raghunandan Feb 21 '14 at 16:53

4 Answers4

0

If it were a view openSettings(MenuItem item) should be openSettings(View view)

But as it is a menu there are special methods in the activity to handle the action. you don't set on click listeners yourself, you override onMenuItemSelected(int featureId, MenuItem item)

(Of course this means you need to remove onClick from the XML completely)

Nick Cardoso
  • 20,807
  • 14
  • 73
  • 124
  • I want to use the onClick method. That's the whole point here. I'm sure I could hardcode a response into the java but I don't want to and it's bad for dev flexibility down the road. I've seen this work before. I'm just confused why it's not working now... – JRad the Bad Feb 21 '14 at 16:31
  • @user3250023 do you have `android:onClick="openSettings"` anywhere other than meunu xml? – Raghunandan Feb 21 '14 at 16:57
  • @user3250023 i don't see much wrong with the code. just rename `Intent openSettings` to something else and clean and build. i tested on api level 19 and it works – Raghunandan Feb 21 '14 at 17:06
  • I've found the solution: There is no solution. It's a bug in the 14/15 API's apparently. Never got fixed for those APIs. :/ – JRad the Bad Feb 21 '14 at 17:11
  • @user3250023 bug. wait a minute i will test it – Raghunandan Feb 21 '14 at 17:12
0

This is not an issue with event handling. The code is fine. The real problem, as it turns out, is Android 4.0.4 API 15. It appears that not only does it fail on my device, but I've found evidence elsewhere that others have similar issues with API 14/15 with onClick event handling in the menu XML.

Please note, this is an issue with onClick in the Menu ONLY. onClick works fine with views.

The solution? You're forced to override onMenuItemSelected() despite how much you might really want to use the onClick menu attribute.

Good luck to others with the same problem!

JRad the Bad
  • 511
  • 5
  • 25
  • i can duplicate the same on api level 15. this isn't a bug – Raghunandan Feb 21 '14 at 17:18
  • Did you use the AVD to test? Note that 4.0.4 I don't think is an option for the emulator. At least it isn't for me... – JRad the Bad Feb 21 '14 at 17:24
  • 4.03 api level 15 on emulator – Raghunandan Feb 21 '14 at 17:25
  • I found a couple of other instances where people on API's 14/15 have this same issue. Note that 4.0.4 was a T-Mobile update pushed out to devices to patch an exploit and prevent rooting devices. It may simply be that my device, being a patched device by T-Mobile, just doesn't work with it. The AVD and other devices are working find this event handling but apparently not every device will. So I'm forced to change it. Thanks for your help Raghunandan. – JRad the Bad Feb 21 '14 at 17:34
  • onMenuItemSelected is exactly the answer I gave you earlier, as well as explaining it was an issue with Menu not Views. – Nick Cardoso Aug 06 '15 at 14:08
  • Coming back over a year later to complain that I didn't give you credit for something is bad practice. Additionally, I didn't ask for a workaround, I asked for help in identifying what was failing. (System bug as it turns out). So no, your answer did not address my concerns and did not merit the "solution" tag. If the community feels otherwise, they are free to upvote your answer. As the OP, it didn't address my original concern and Raghunandan was of more assistance in tracking down the issue than yourself. – JRad the Bad Aug 07 '15 at 14:51
0

I think as you say, it is a bug. I had this problem when I used ActionBarActivity. But I changed ActionBarActivity to FragmentActivity, this problem is "solved".

Hope this could help with your problem.

0

To make the onClick listener work in XML, you need to add two properties: android:clickable="true" android:onClick="myFunction"

Both of these properties are necessary to make the onClick listener work. But in terms of their XML properties, menu items are only checkable and not clickable.

Android Studio doesn't show you android:clickable as one of the suggestions for a menu item, when you are typing it in XML. It only shows android:checkable.

This isn't a bug. It's designed to work this way.

You can still attach an onClick listener to a menu item, but you need to do it in java code.

  • Mike, this a year old but I suggest you read the entirety of the conversation here. The issue was NOT the manifest of the XML view. The same code run on a separate device worked just fine. Older devices and new devices executed the code as expected. The only android API that had an issue with executing the code as posted above was 4.0.4 which is the Hercules update specific to T-Mobile devices. Again, the code was fine, the issue was a conflict with that particular API. – JRad the Bad Jan 06 '16 at 08:01
  • For additional information regarding this known BUG, see the following: 1. http://stackoverflow.com/questions/11245829/inflateexception-couldnt-resolve-menu-item-onclick-handler 2. http://stackoverflow.com/questions/21457929/androidonclick-doesnt-work-with-android-4-0-3 – JRad the Bad Jan 06 '16 at 08:05