WearableListView ClassNotFoundException Support v4

2019-12-16 21:58发布

问题:

Ok this is a fun one: I'm using Android Studio 1.1.0, I have an Android app and a correlating Android Wear app i'm trying to test on a smart watch device. I've placed the class files I used in my Android app into an Android library module then added it as a dependency into my Android Wear app. The app builds and deploys onto the smart watch but when it runs I get the below exception and stack trace. Any suggestions would be appreciated. Thank you!

04-17 21:55:45.764    7759-7759/? E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.myapp.wear, PID: 7759
    android.view.InflateException: Binary XML file line #12: Error inflating class android.support.wearable.view.WearableListView
            at android.view.LayoutInflater.createView(LayoutInflater.java:633)
            at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:743)
            at android.view.LayoutInflater.rInflate(LayoutInflater.java:806)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:504)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:365)
            at android.support.wearable.view.WatchViewStub.inflate(WatchViewStub.java:179)
            at android.support.wearable.view.WatchViewStub.onApplyWindowInsets(WatchViewStub.java:148)
            at android.view.View.dispatchApplyWindowInsets(View.java:6514)
            at android.view.ViewGroup.dispatchApplyWindowInsets(ViewGroup.java:5782)
            at android.view.ViewGroup.dispatchApplyWindowInsets(ViewGroup.java:5786)
            at android.view.ViewGroup.dispatchApplyWindowInsets(ViewGroup.java:5786)
            at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchApplyWindowInsets(PhoneWindow.java:2333)
            at android.view.ViewRootImpl.dispatchApplyInsets(ViewRootImpl.java:1205)
            at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1423)
            at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1054)
            at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5779)
            at android.view.Choreographer$CallbackRecord.run(Choreographer.java:767)
            at android.view.Choreographer.doCallbacks(Choreographer.java:580)
            at android.view.Choreographer.doFrame(Choreographer.java:550)
            at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:753)
            at android.os.Handler.handleCallback(Handler.java:739)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5221)
            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:899)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
     Caused by: java.lang.reflect.InvocationTargetException
            at java.lang.reflect.Constructor.newInstance(Native Method)
            at java.lang.reflect.Constructor.newInstance(Constructor.java:288)
            at android.view.LayoutInflater.createView(LayoutInflater.java:607)
            at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:743)
            at android.view.LayoutInflater.rInflate(LayoutInflater.java:806)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:504)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:365)
            at android.support.wearable.view.WatchViewStub.inflate(WatchViewStub.java:179)
            at android.support.wearable.view.WatchViewStub.onApplyWindowInsets(WatchViewStub.java:148)
            at android.view.View.dispatchApplyWindowInsets(View.java:6514)
            at android.view.ViewGroup.dispatchApplyWindowInsets(ViewGroup.java:5782)
            at android.view.ViewGroup.dispatchApplyWindowInsets(ViewGroup.java:5786)
            at android.view.ViewGroup.dispatchApplyWindowInsets(ViewGroup.java:5786)
            at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchApplyWindowInsets(PhoneWindow.java:2333)
            at android.view.ViewRootImpl.dispatchApplyInsets(ViewRootImpl.java:1205)
            at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1423)
            at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1054)
            at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5779)
            at android.view.Choreographer$CallbackRecord.run(Choreographer.java:767)
            at android.view.Choreographer.doCallbacks(Choreographer.java:580)
            at android.view.Choreographer.doFrame(Choreographer.java:550)
            at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:753)
            at android.os.Handler.handleCallback(Handler.java:739)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5221)
            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:899)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
     Caused by: java.lang.NoClassDefFoundError: Failed resolution of: Landroid/support/v4/util/ArrayMap;
            at android.support.v7.widget.RecyclerView$State.<init>(RecyclerView.java:7461)
            at android.support.v7.widget.RecyclerView.<init>(RecyclerView.java:233)
            at android.support.wearable.view.WearableListView.<init>(WearableListView.java:151)
            at android.support.wearable.view.WearableListView.<init>(WearableListView.java:147)
            at java.lang.reflect.Constructor.newInstance(Native Method)
            at java.lang.reflect.Constructor.newInstance(Constructor.java:288)
            at android.view.LayoutInflater.createView(LayoutInflater.java:607)
            at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:743)
            at android.view.LayoutInflater.rInflate(LayoutInflater.java:806)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:504)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:365)
            at android.support.wearable.view.WatchViewStub.inflate(WatchViewStub.java:179)
            at android.support.wearable.view.WatchViewStub.onApplyWindowInsets(WatchViewStub.java:148)
            at android.view.View.dispatchApplyWindowInsets(View.java:6514)
            at android.view.ViewGroup.dispatchApplyWindowInsets(ViewGroup.java:5782)
            at android.view.ViewGroup.dispatchApplyWindowInsets(ViewGroup.java:5786)
            at android.view.ViewGroup.dispatchApplyWindowInsets(ViewGroup.java:5786)
            at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchApplyWindowInsets(PhoneWindow.java:2333)
            at android.view.ViewRootImpl.dispatchApplyInsets(ViewRootImpl.java:1205)
            at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1423)
            at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1054)
            at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5779)
            at android.view.Choreographer$CallbackRecord.run(Choreographer.java:767)
            at android.view.Choreographer.doCallbacks(Choreographer.java:580)
            at android.view.Choreographer.doFrame(Choreographer.java:550)
            at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:753)
            at android.os.Handler.handleCallback(Handler.java:739)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5221)
            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:899)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
     Caused by: java.lang.ClassNotFoundException: Didn't find class "android.support.v4.util.ArrayMap" on path: DexPathList[[zip file "/data/app/com.myapp.wear-1/base.apk"],nativeLibraryDirectories=[/data/app/com.myapp.wear-1/lib/arm, /vendor/lib, /system/lib]]
            at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
            at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
            at java.lang.ClassLoader.loadClass(ClassLoader.java:469)
            at android.support.v7.widget.RecyclerView$State.<init>(RecyclerView.java:7461)
            at android.support.v7.widget.RecyclerView.<init>(RecyclerView.java:233)
            at android.support.wearable.view.WearableListView.<init>(WearableListView.java:151)
            at android.support.wearable.view.WearableListView.<init>(WearableListView.java:147)
            at java.lang.reflect.Constructor.newInstance(Native Method)
            at java.lang.reflect.Constructor.newInstance(Constructor.java:288)
            at android.view.LayoutInflater.createView(LayoutInflater.java:607)
            at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:743)
            at android.view.LayoutInflater.rInflate(LayoutInflater.java:806)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:504)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:365)
            at android.support.wearable.view.WatchViewStub.inflate(WatchViewStub.java:179)
            at android.support.wearable.view.WatchViewStub.onApplyWindowInsets(WatchViewStub.java:148)
            at android.view.View.dispatchApplyWindowInsets(View.java:6514)
            at android.view.ViewGroup.dispatchApplyWindowInsets(ViewGroup.java:5782)
            at android.view.ViewGroup.dispatchApplyWindowInsets(ViewGroup.java:5786)
            at android.view.ViewGroup.dispatchApplyWindowInsets(ViewGroup.java:5786)
            at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchApplyWindowInsets(PhoneWindow.java:2333)
            at android.view.ViewRootImpl.dispatchApplyInsets(ViewRootImpl.java:1205)
            at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1423)
            at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1054)
            at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5779)
            at android.view.Choreographer$CallbackRecord.run(Choreographer.java:767)
            at android.view.Choreographer.doCallbacks(Choreographer.java:580)
            at android.view.Choreographer.doFrame(Choreographer.java:550)
            at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:753)
            at android.os.Handler.handleCallback(Handler.java:739)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5221)
            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:899)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
    Suppressed: java.lang.ClassNotFoundException: android.support.v4.util.ArrayMap
            at java.lang.Class.classForName(Native Method)
            at java.lang.BootClassLoader.findClass(ClassLoader.java:781)
            at java.lang.BootClassLoader.loadClass(ClassLoader.java:841)
            at java.lang.ClassLoader.loadClass(ClassLoader.java:504)
            ... 36 more
     Caused by: java.lang.NoClassDefFoundError: Class not found using the boot class loader; no stack available

Here's a visual of the gradle configs and modules:

回答1:

I saw this java.lang.ClassNotFoundException: android.support.v4.util.ArrayMap in your logcat trace. And I think maybe the version of your v4 and v7 cause this error. Is your support library of v4 and v7 is up to date? And BTW, ArrayMap only could be used for Android API 21+



回答2:

In my case , I solved this issue by deleting the android-support-v4.jar because it's old and has not actually ArrayMap.class and I put the one from \extras\android\support\v4 in my lib folder and then add it to Java build path and it worked finally. Hope it helps :)



回答3:

My case was after upgrading

    compile 'com.google.android.gms:play-services:8.1.0'

to

    compile 'com.google.android.gms:play-services:8.3.0'

Going back to 8.1.0 solved it for me



回答4:

You can find a solution to this error here.

The error is very clear (basically ArrayMap is missing):

java.lang.NoClassDefFoundError: Failed resolution of: Landroid/support/v4/util/ArrayMap;

This happens because the support library version is old, and another library (presumably Google Play Services) doesn't find ArrayMap on it.

The solution is to upgrade the support library to a new version that includes ArrayMap. That's what I did and it worked.

In addition, according the linked solution you should also:

make sure that your compileSdkVersion matches the major version number of the dependency