java.lang.UnsupportedOperationException:不支持此操作,请使用MenuItemCompat.setOnActionExpandListener()。

22

我最近从Eclipse迁移到Android Studio,在这个过程中我遇到了以下错误:

java.lang.UnsupportedOperationException: This is not supported, use MenuItemCompat.setOnActionExpandListener()
        at android.support.v7.internal.view.menu.MenuItemImpl.setOnActionExpandListener(MenuItemImpl.java:740)
        at biz.nickbullcomputing.bevnav.MainActivity.onCreateOptionsMenu(MainActivity.java:699)
        at android.app.Activity.onCreatePanelMenu(Activity.java:2851)
        at android.support.v4.app.FragmentActivity.onCreatePanelMenu(FragmentActivity.java:277)
        at android.support.v7.internal.view.WindowCallbackWrapper.onCreatePanelMenu(WindowCallbackWrapper.java:84)
        at android.support.v7.app.AppCompatDelegateImplBase$AppCompatWindowCallbackBase.onCreatePanelMenu(AppCompatDelegateImplBase.java:273)
        at android.support.v7.app.AppCompatDelegateImplV7.preparePanel(AppCompatDelegateImplV7.java:1111)
        at android.support.v7.app.AppCompatDelegateImplV7.doInvalidatePanelMenu(AppCompatDelegateImplV7.java:1396)
        at android.support.v7.app.AppCompatDelegateImplV7.access$100(AppCompatDelegateImplV7.java:89)
        at android.support.v7.app.AppCompatDelegateImplV7$1.run(AppCompatDelegateImplV7.java:126)
        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:5725)
        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:1030)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:825)

这个错误似乎来自于我的主活动的以下代码片段

 searchItem = menu.findItem(R.id.action_search);

     searchItem.setOnActionExpandListener(new OnActionExpandListener()
     {
         @Override
         public boolean onMenuItemActionCollapse(MenuItem item) {
             townList.setVisibility(View.INVISIBLE);
             return true;       // Return true to collapse action view
         }
         @Override
         public boolean onMenuItemActionExpand(MenuItem item) {
             townList.setVisibility(View.VISIBLE);
             return true;      // Return true to expand action view
         }
     });

搜索的XML代码:

<item android:id="@+id/action_search"
    android:icon="@drawable/ic_action_search"
    android:title="@string/action_search"
    app:showAsAction="ifRoom|collapseActionView"
    app:actionViewClass="android.support.v7.widget.SearchView"/>

我 build.gradle 文件中的依赖项

dependencies {
compile 'com.android.support:support-v4:22.2.1'
compile 'com.android.support:appcompat-v7:22.0.0'
compile 'com.google.android.gms:play-services:+'

迁移之前,记住这个功能是完美运行的,现在却不行了。我不确定发生了什么。有什么想法吗?


4
请使用MenuItemCompat.OnActionExpandListener代替MenuItemImpl.OnActionExpandListener。 - ρяσѕρєя K
我尝试了这个,但是出现了一个“方法调用预期”的错误,我是不是在某个地方漏掉了导入? - Nick Bull
哦,等等,我明白了,谢谢@ρяσѕρєя K,你解决了问题。 - Nick Bull
2个回答

63

多亏了ρяσѕρєя K的评论,问题得以解决。非常感激,谢谢!!

MenuItemCompat.setOnActionExpandListener(searchItem,
            new MenuItemCompat.OnActionExpandListener() {
                @Override
                public boolean onMenuItemActionExpand(MenuItem menuItem) {
                    // Return true to allow the action view to expand
                    townList.setVisibility(View.VISIBLE);
                    return true;
                }
                @Override
                public boolean onMenuItemActionCollapse(MenuItem menuItem) {
                    // When the action view is collapsed, reset the query
                    townList.setVisibility(View.INVISIBLE);
                    // Return true to allow the action view to collapse
                    return true;
                }
            });

10
我正在使用完全相同的类和方法,但它仍然无法工作!:( - Virat18

0

我认为你需要检查编译依赖,因为 setOnActionExpandListener 26.1.0 已经被弃用了,请检查你的 app/build.gradle 。我曾经遇到过同样的问题,以下是配置帮助了我。希望这能对你有所帮助。

以下是我的 build.gradle 配置:

compileSdkVersion 25
buildToolsVersion "25.0.2"
targetSdkVersion 25

compile 'com.android.support:support-v4:25.3.1'
compile 'com.android.support:recyclerview-v7:25.3.1'
compile 'com.android.support:cardview-v7:25.3.1'
compile 'com.android.support:support-vector-drawable:25.3.1'
compile 'com.android.support:design:25.3.1'

MenuItemCompat.setOnActionExpandListener(item,
                new MenuItemCompat.OnActionExpandListener() {
                    @Override
                    public boolean onMenuItemActionExpand(MenuItem menuItem) {
                        // Return true to allow the action view to expand
                        return true;
                    }

                    @Override
                    public boolean onMenuItemActionCollapse(MenuItem menuItem) {
                        // When the action view is collapsed, reset the query
                        // Return true to allow the action view to collapse
                        return false;
                    }
                });

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接