2015-08-23 76 views
22

我最近從Eclipse的移植到Android Studio和這樣做我拿起下面的錯誤java.lang.UnsupportedOperationException:這是不支持的,使用MenuItemCompat.setOnActionExpandListener()

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' –

+0

我都試過,但得到'方法調用Expected'的錯誤我在什麼地方丟失的進口? – TaurusDroid

+0

噢,我已經得到了它謝謝@ρяσѕρєяK做了竅門 – TaurusDroid

回答

61

該修復感謝ρяσѕρєя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; 
       } 
      }); 
+8

我使用相同的確切類和方法,但它仍然不工作! :( – Virat18

0

我認爲你必須檢查你的編譯依賴關係,因爲它是在setOnActionExpandListener 26.1.0棄用檢查您的應用程序/的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; 
        } 
       }); 
相關問題