InflateException: Couldn't resolve menu item onClick handler

后端 未结 7 764
情书的邮戳
情书的邮戳 2020-12-08 19:12

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

相关标签:
7条回答
  • 2020-12-08 19:44

    In my case the problem was that I had both onClick in my menu XML and an onCreateOptionsMenu in my Activity. My onClick was actually faulty (because it pointed to non-existent methods) but I didn't notice this at first because I was testing under Android 2.x, where onClick is not supported and ignored. Once I tested on 4.x though, I started getting this error.

    So basically, don't use onClick if you plan on deploying under Android 2.x. It will silently ignore your onClick values until you try running on 3.0+.

    0 讨论(0)
  • 2020-12-08 19:50

    Although this is a bit out of date, here is the reason for the exception. When you look into the sources of android API 15 (4.0.3-4.0.4) in the class MenuInflater you will see this method:

    public InflatedOnMenuItemClickListener(Context context, String methodName) {
    mContext = context;
    Class<?> c = context.getClass();
    try {
        mMethod = c.getMethod(methodName, PARAM_TYPES);
    } catch (Exception e) {
        InflateException ex = new InflateException(
                "Couldn't resolve menu item onClick handler " + methodName +
                " in class " + c.getName());
        ex.initCause(e);
        throw ex;
    }
    

    This is were the exception happens, as Junique already pointed out. However the removing of the app theme is just a workaround and no real option. As we see the method tries to find the Callback method on the class of the context item passed. So instead of calling getMenuInflater() in onCreateOptionsMenu you should call new MenuInflater(this), so that this is passed as a context and then the code will work.

    You can still use getMenuInflater() for other api versions if you just use an if statement like this:

    if (Build.VERSION.SDK_INT > 15)
            inflater = getMenuInflater();
        else
            inflater = new MenuInflater(this);
    

    I don't actually know if the bug happens in api versions under 15 too, so i just generally used the save version.

    0 讨论(0)
  • 2020-12-08 19:52
    @Override
    public boolean onCreateOptionsMenu(Menu menu) 
    {
        getMenuInflater().inflate(R.menu.activity_main, menu);
    
        MenuItem item = menu.findItem(R.id.menu_open);
    
        if (item == null)
            return true;
    
        item.setOnMenuItemClickListener
        (
            new MenuItem.OnMenuItemClickListener () 
            { 
                public boolean onMenuItemClick(MenuItem item) 
                { return (showDirectory(item)); }
            } 
        ); 
    
        return true;
    }
    
    
    public boolean showDirectory (MenuItem item)
    {
        CheckBox checkBox = (CheckBox) findViewById (R.id.checkBox1);
        checkBox.setChecked(true);
    }
    
    0 讨论(0)
  • 2020-12-08 19:56

    In my case, the AndroidManifest.xml of my application (kick-started by the default Eclipse assistant) contained android:theme="@style/AppTheme" in the <application> block.

    When debugging the cause of the problem, it turned out that the line

    mMethod = c.getMethod(methodName, PARAM_TYPES);
    

    in android.view.MenuInflater/InflatedOnMenuItemClickListener was called with c not being my Activity class but a dubious android.view.ContextThemeWrapper (which of course doesn't contain the onClick handler).

    So, I removed the android:theme and everything worked.

    0 讨论(0)
  • 2020-12-08 19:59

    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.

    0 讨论(0)
  • 2020-12-08 20:00

    Your method must accept a MenuItem as its only parameter per here.

        public void onMenuItemClickMethod(MenuItem menuItem){
            // Do stuff here
        }
    
    0 讨论(0)
提交回复
热议问题