java.lang.NoSuchMethodError: No static method isCt

2019-07-24 17:23发布

I received the following Stack Trace in Android Studio (v.2.2.3):

E/InputEventSender: Exception dispatching finished signal.
E/MessageQueue-JNI: Exception in MessageQueue callback: handleReceiveCallback
E/MessageQueue-JNI: java.lang.NoSuchMethodError: No static method isCtrlPressed(Landroid/view/KeyEvent;)Z in class Landroid/support/v4/view/KeyEventCompat; or its super classes (declaration of 'android.support.v4.view.KeyEventCompat' appears in /data/app/com.app.name-2/base.apk) at android.support.v7.app.AppCompatActivity.dispatchKeyEvent(AppCompatActivity.java:526)
at android.support.v7.view.WindowCallbackWrapper.dispatchKeyEvent(WindowCallbackWrapper.java:59)
at android.support.v7.app.AppCompatDelegateImplBase$AppCompatWindowCallbackBase.dispatchKeyEvent(AppCompatDelegateImplBase.java:319)
at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchKeyEvent(PhoneWindow.java:2276)
at android.view.ViewRootImpl$ViewPostImeInputStage.processKeyEvent(ViewRootImpl.java:4020)
at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:3982)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3544)
at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3597)
at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3563)
at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:3680)
at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3571)
at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:3737)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3544)
at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3597)    at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3563)
at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3571)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3544)
at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3597)
at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3563)
at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:3713)     
at android.view.ViewRootImpl$ImeInputStage.onFinishedInputEvent(ViewRootImpl.java:3874)
at android.view.inputmethod.InputMethodManager$PendingEvent.run(InputMethodManager.java:2208)
at android.view.inputmethod.InputMethodManager.invokeFinishedInputEventCallback(InputMethodManager.java:1849)
at android.view.inputmethod.InputMethodManager.finishedInputEvent(InputMethodManager.java:1840)
at android.view.inputmethod.InputMethodManager$ImeInputEventSender.onInputEventFinished(InputMethodManager.java:2185)
at android.view.InputEventSender.dispatchInputEventFinished(InputEventSender.java:141)
at android.os.MessageQueue.nativePollOnce(Native Method)
at android.os.MessageQueue.next(MessageQueue.java:143)
at android.os.Looper.loop(Looper.java:122)
at android.app.ActivityThread.main(ActivityThread.java:5254)
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:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)

The android.support.v4.view package has a KeyEventCompat class which has a documented boolean member variable, isCtrlPressed.

As my app's views override the AppCompatActivity, the dispatchKeyEvent() function statically calls KeyEventCompat.isCtrlPressed(), but the function does not exist.

I have the following gradle settings:

compileSdkVersion = 25
buildToolsVersion = "25.0.2"
supportLibVersion = "25.2.0"
minSdkVersion 14
targetSdkVersion = 25

With the following dependencies:

compile "com.android.support:appcompat-v7:25.2.0"
compile "com.android.support:support-compat:25.2.0"
compile "com.android.support:support-media-compat:25.2.0"
compile "com.android.support:gridlayout-v7:25.2.0"
compile "com.android.support:support-core-utils:${rootProject.ext.supportLibVersion}"
compile "com.android.support:support-core-ui:${rootProject.ext.supportLibVersion}"
compile "com.android.support:design:${rootProject.ext.supportLibVersion}"
compile "com.android.support:recyclerview-v7:${rootProject.ext.supportLibVersion}"
compile 'com.android.support:support-v4:25.2.0'

Is this an Android bug? If so, what is the suggested work around? Perhaps try to compile using a lower version of build tools & support library?

1条回答
时光不老,我们不散
2楼-- · 2019-07-24 17:52

It should be noted that the SO Issue located here did not solve my issue.

I have filed this Issue as an AOSP Bug #257553
java.lang.NoSuchMethodError: No static method isCtrlPressed(Landroid/view/KeyEvent;)Z in class Landroid/support/v4/view/KeyEventCompat;

Thanks to the brilliant write up on Codepath - Migrating To The AppCompat Library I have followed a reverse version of the documented events on this page in order to downgrade the AppCompat & Support Libraries from version 25.2.0 to version 25.1.0 (Yes, I stepped down two versions, just to be safe. You could try stepping down 1 version if you so please).

So, the steps I took to work around the Issue were to:

  1. Remove BuildTools 25.0.2 from the SDK Manager;
  2. Find the appcompat-v7 SDK folder and delete the entire 25.2.0 and in my case the 25.1.1 folder, as I decided to step down two versions;

    File location for Mac OS users:

      /users/[username]/Library/Android/Sdk/extras/android/m2repository/com/android/support/appcompat-v7 
    

    File location for PC users:

    C:\Documents and Settiings<user>\ApPData\Local\Android\Sdk\extras\android\m2repository\com\android\support\appcompat-v7
    
  3. Inside this same folder (location defined in Step 2 above) edit the maven-metadata.xml file and delete the two lines which contain:

    <version>25.2.0</version>  
    <version>25.1.1</version>  
    
  4. Downgrade the Build Tools and AppCompat Library in app/build.gradle:

    buildToolsVersion '25.0.0'
    supportLibVersion '25.1.0'
    
  5. Clean the project and rebuild.

UPDATE

It should be noted that the above solution worked for those particular library versions and build variant.

As soon as I updated the following build variables, the original error returned again:

buildTooleVersion '25.0.2'
supportLibVersion '25.3.0'

The solution that solved the problem under this build variant is quite simple and can be found here. In fact, I would assume that this should be the correct answer under all conditions but I have not tested this in all build variants.

To summarise the referenced solution, instead of subclassing the android.support.v7.app.AppCompatActivity, an application's Activity should subclass the android.app.Activity class. Whilst if you are creating a custom View for use in that Activity, the custom View should subclass the android.support.v7.widget View. By doing so, the view will inherit the support features when using that View inside the Activity class.

查看更多
登录 后发表回答