问题
Today Android Studio (set to Stable channel updates) has offered me to download an update to Android Support Repository 46.0.0, so I did it. Then suddenly our app has started to crash each time because of MenuItemCompat.setOnActionExpandListener.
Yes I've should have check release notes yet, but I did not - that's a fact.
Now what's our problem - Even we're using recommended notation MenuItemCompat.setOnActionExpandListener, our app crashed and we're advised to to use MenuItemCompat.setOnActionExpandListener. That's nonsense.
Is there someone who run into the same issue or is it just a matter of our code? Can someone provide an advice how to fix this or how to force to use earlier version of support library? Let's say 25.1.0. Issue seems to be in 26.0.0-alpha1 which is very likely included in Support Repository version 46 (see Release notes for that repository - https://developer.android.com/topic/libraries/support-library/revisions.html#26-0-0-alpha1)
Btw. our build.gradle already states fix version for support libs to 25.1.0. It seems new support library somehow forces to use other versions internally when built with gradle.
compile 'com.android.support:appcompat-v7:25.1.0'
compile 'com.android.support:design:25.1.0'
compile 'com.android.support:support-v4:25.1.0'
Thank you very much.
Code
MenuItemCompat.setOnActionExpandListener(menu.findItem(R.id.action_search), new MenuItemCompat.OnActionExpandListener() {
@Override
public boolean onMenuItemActionExpand(MenuItem menuItem) {
...
}
Part of StackTrace
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.dev, PID: 4835
java.lang.UnsupportedOperationException: This is not supported, use MenuItemCompat.setOnActionExpandListener()
at android.support.v7.view.menu.MenuItemImpl.setOnActionExpandListener(MenuItemImpl.java:745)
at android.support.v4.view.MenuItemCompat.setOnActionExpandListener(MenuItemCompat.java:363)
at com.example.fragments.DevicesFragment.onCreateOptionsMenu(DevicesFragment.java:455)
at android.support.v4.app.Fragment.performCreateOptionsMenu(Fragment.java:2338)
at android.support.v4.app.FragmentManagerImpl.dispatchCreateOptionsMenu(FragmentManager.java:3070)
at android.support.v4.app.FragmentController.dispatchCreateOptionsMenu(FragmentController.java:328)
at android.support.v4.app.FragmentActivity.onCreatePanelMenu(FragmentActivity.java:321)
at android.support.v7.view.WindowCallbackWrapper.onCreatePanelMenu(WindowCallbackWrapper.java:95)
at android.support.v7.app.AppCompatDelegateImplBase$AppCompatWindowCallbackBase.onCreatePanelMenu(AppCompatDelegateImplBase.java:333)
at android.support.v7.app.AppCompatDelegateImplV9.preparePanel(AppCompatDelegateImplV9.java:1372)
at android.support.v7.app.AppCompatDelegateImplV9.doInvalidatePanelMenu(AppCompatDelegateImplV9.java:1652)
at android.support.v7.app.AppCompatDelegateImplV9$1.run(AppCompatDelegateImplV9.java:134)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
UPDATE: This seems to be connected to this issue, since I've tried that also and same thing happened to me. I do not want to update to API 26 anyway - Android Support Repo 46.0.0 with Android Studio 2.3
回答1:
It is just a bug in the 26.0.0-alpha1 release. Switch back to 25.3.0 by updating your build.gradle
file.
回答2:
UPDATE 23.3.2017: Based on comments and further research the right way is really to force gradle to use explicit version when Gradle dependency resolution makes you to use any other version than desired.
Thanks to @Eugen and @ianhanniballake you can use following steps to decide if any action is needed and force the explicit version.
- Run
gradlew androidDependencies
to check which versions are resolved and used by Gradle - If it does not match your required explicit version, force it as stated by @Eugen on this topic - https://stackoverflow.com/a/42957234/816216 (snippet posted below)
_
configurations.all {
resolutionStrategy.eachDependency { DependencyResolveDetails details ->
def requested = details.requested
if (requested.group == 'com.android.support') {
if (!requested.name.startsWith("multidex")) {
details.useVersion '25.3.0'
}
}
}
}
来源:https://stackoverflow.com/questions/42961535/even-im-using-menuitemcompat-setonactionexpandlistener-my-app-crashes-with-advi