setOnActionExpandListener

setOnActionExpandListener

我以前成功创建了一个搜索项目并调用了.setOnActionExpandListener(),但是现在我收到了UnsupportedOperationException,并且不知道如何解决。 compileSDKVersiontargetSDKVersion之间的冲突可能与它有关,但我不确定。我读过其他SO帖子,指出它实际上是Google's support library中的错误,并且已得到修复,但是我无法确认。我最近更新了我正在使用的Android支持库,这也可能是导致该问题的原因,请参见以下详细信息:

 @Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.menu_search, menu);
    // Retrieve the SearchView and plug it into SearchManager

    SupportMenuItem searchItem = (SupportMenuItem) menu.findItem(R.id.search);

    final SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchItem);


    searchItem.setOnActionExpandListener(new MenuItem.OnActionExpandListener() {
        @Override
        public boolean onMenuItemActionExpand(MenuItem menuItem) {
            Toast.makeText(getApplicationContext(), "OPEN", Toast.LENGTH_LONG).show();

            return false;
        }

        @Override
        public boolean onMenuItemActionCollapse(MenuItem menuItem) {
            Toast.makeText(getApplicationContext(), "Close", Toast.LENGTH_LONG).show();
            finish();
            return false;
        }
    });


例外:

12-03 17:31:55.743 18797-18797/com.troychuinard.fanpolls E/AndroidRuntime: FATAL EXCEPTION: main
                                                                       Process: com.troychuinard.fanpolls, PID: 18797
                                                                       java.lang.UnsupportedOperationException: This is not supported, use MenuItemCompat.setOnActionExpandListener()
                                                                           at android.support.v7.view.menu.MenuItemImpl.setOnActionExpandListener(MenuItemImpl.java:743)
                                                                           at com.troychuinard.fanpolls.NewImageActivity.onCreateOptionsMenu(NewImageActivity.java:90)
                                                                           at android.app.Activity.onCreatePanelMenu(Activity.java:2881)
                                                                           at android.support.v4.app.FragmentActivity.onCreatePanelMenu(FragmentActivity.java:328)
                                                                           at android.support.v7.view.WindowCallbackWrapper.onCreatePanelMenu(WindowCallbackWrapper.java:98)
                                                                           at android.support.v7.app.AppCompatDelegateImplBase$AppCompatWindowCallbackBase.onCreatePanelMenu(AppCompatDelegateImplBase.java:335)
                                                                           at android.support.v7.app.AppCompatDelegateImplV9.preparePanel(AppCompatDelegateImplV9.java:1368)
                                                                           at android.support.v7.app.AppCompatDelegateImplV9.doInvalidatePanelMenu(AppCompatDelegateImplV9.java:1648)
                                                                           at android.support.v7.app.AppCompatDelegateImplV9$1.run(AppCompatDelegateImplV9.java:140)
                                                                           at android.os.Handler.handleCallback(Handler.java:739)
                                                                           at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                           at android.os.Looper.loop(Looper.java:148)
                                                                           at android.app.ActivityThread.main(ActivityThread.java:5527)
                                                                           at java.lang.reflect.Method.invoke(Native Method)
                                                                           at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:730)
                                                                           at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:620)


应用程式Gradle:

apply plugin: 'com.android.application'

  android {
compileSdkVersion 25
buildToolsVersion '26.0.2'

defaultConfig {
    vectorDrawables.useSupportLibrary = true
    applicationId "xxxxxxxx"
    minSdkVersion 16
    targetSdkVersion 25
    versionCode 1
    versionName "1.0"
    multiDexEnabled true

}

dexOptions {
    javaMaxHeapSize "2g"
}

buildTypes {
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
  }
}

dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')

// Support library
compile "com.android.support:design:$supportLibVersion"
compile "com.android.support:cardview-v7:$supportLibVersion"
compile "com.android.support:support-v4:$supportLibVersion"


// Firebase
compile "com.google.firebase:firebase-storage:$playServicesVersion"
 //    compile 'com.github.SUPERCILEX.FirebaseUI-Android:firebase-ui-
database:e3c0feaddae3c7cc0fc7b3333bf3c4c11e4ae406'
compile "com.google.firebase:firebase-core:$playServicesVersion"
compile "com.google.firebase:firebase-auth:$playServicesVersion"
compile "com.google.android.gms:play-services-
 auth:$playServicesVersion"
compile "com.google.firebase:firebase-database:$playServicesVersion"
compile "com.google.firebase:firebase-appindexing:$playServicesVersion"
    //    compile "com.google.firebase:firebase-
firestore:$playServicesVersion"
 //    compile 'com.firebaseui:firebase-ui-firestore:3.1.0'
compile 'com.firebaseui:firebase-ui-database:3.1.0'


// Third party
compile 'com.android.support.constraint:constraint-layout:1.0.2'
compile 'com.facebook.android:facebook-android-sdk:4.+'
compile 'jp.wasabeef:recyclerview-animators:2.2.3'
compile 'com.github.clans:fab:1.6.2'
compile 'de.hdodenhof:circleimageview:2.0.0'
compile 'com.jakewharton:butterknife:7.0.1'
compile 'com.squareup.picasso:picasso:2.5.2'
compile 'com.squareup.okhttp3:okhttp:3.1.2'
compile 'com.github.PhilJay:MPAndroidChart:v2.2.2'
compile 'com.google.code.gson:gson:2.6.2'
compile 'com.squareup.retrofit2:retrofit:2.1.0'
compile 'com.squareup.retrofit2:converter-gson:2.1.0'
compile 'com.squareup.okhttp3:logging-interceptor:3.3.0'
compile 'com.android.support:multidex:1.0.1'
 }

apply plugin: 'com.google.gms.google-services'


根摇篮:

buildscript {
repositories {
    jcenter()
    maven {
        url 'https://maven.google.com/'
        name 'Google'
    }
}

dependencies {
    classpath 'com.android.tools.build:gradle:2.2.3'
    classpath 'com.google.gms:google-services:3.1.0'
     }
 }

 allprojects {
    repositories {
     jcenter()
    maven { url 'https://maven.fabric.io/public' }
    maven { url 'https://jitpack.io' }
    maven { url "https://maven.google.com" }

    maven {
        url 'https://maven.google.com/'
        name 'Google'
    }
}
  }

task clean(type: Delete) {
delete rootProject.buildDir
  }

ext {
supportLibVersion = "25.3.1"
playServicesVersion = "11.4.2"
  }

最佳答案

错误日志很好地描述了异常原因:


  不支持此功能,请使用MenuItemCompat.setOnActionExpandListener()


这意味着,而不是:

searchItem.setOnActionExpandListener(new MenuItem.OnActionExpandListener() {...});


你应该做:

MenuItemCompat.setOnActionExpandListener(searchItem, new MenuItemCompat.OnActionExpandListener() {...})

07-21 23:32