Android X Rejecting re-init on previously-failed class java.lang.Class<androidx.core.view.ViewCompat 2 > : j a v a . l a n g . N o C l a s s D e f F o u n d E r r o r : F a i l e d r e s o l u t i o n o f : L a n d r o i d / v i e w / V i e w 2>: java.lang.NoClassDefFoundError: Failed resolution of: Landroid/view/View 2>:java.lang.NoClassDefFoundError:Failedresolutionof:Landroid/view/ViewOnUnhandledKeyEventListener;
运行android X项目时报错如下,提示找不到OnUnhandledKeyEventListener。
Rejecting re-init on previously-failed class java.lang.Class<androidx.core.view.ViewCompat$2>: java.lang.NoClassDefFoundError: Failed resolution of: Landroid/view/View$OnUnhandledKeyEventListener;
2021-08-19 14:48:58.425 8369-8369/com.demo.androidhtml I/zygote64: at void androidx.core.view.ViewCompat.setBackground(android.view.View, android.graphics.drawable.Drawable) (ViewCompat.java:2678)
2021-08-19 14:48:58.425 8369-8369/com.demo.androidhtml I/zygote64: at void androidx.appcompat.widget.ActionBarContainer.<init>(android.content.Context, android.util.AttributeSet) (ActionBarContainer.java:63)
2021-08-19 14:48:58.425 8369-8369/com.demo.androidhtml I/zygote64: at java.lang.Object java.lang.reflect.Constructor.newInstance0(java.lang.Object[]) (Constructor.java:-2)
2021-08-19 14:48:58.425 8369-8369/com.demo.androidhtml I/zygote64: at java.lang.Object java.lang.reflect.Constructor.newInstance(java.lang.Object[]) (Constructor.java:334)
2021-08-19 14:48:58.425 8369-8369/com.demo.androidhtml I/zygote64: at android.view.View android.view.LayoutInflater.createView(java.lang.String, java.lang.String, android.util.AttributeSet) (LayoutInflater.java:650)
2021-08-19 14:48:58.425 8369-8369/com.demo.androidhtml I/zygote64: at android.view.View android.view.LayoutInflater.createViewFromTag(android.view.View, java.lang.String, android.content.Context, android.util.AttributeSet, boolean) (LayoutInflater.java:797)
2021-08-19 14:48:58.425 8369-8369/com.demo.androidhtml I/zygote64: at android.view.View android.view.LayoutInflater.createViewFromTag(android.view.View, java.lang.String, android.content.Context, android.util.AttributeSet) (LayoutInflater.java:737)
2021-08-19 14:48:58.425 8369-8369/com.demo.androidhtml I/zygote64: at void android.view.LayoutInflater.rInflate(org.xmlpull.v1.XmlPullParser, android.view.View, android.content.Context, android.util.AttributeSet, boolean) (LayoutInflater.java:870)
2021-08-19 14:48:58.425 8369-8369/com.demo.androidhtml I/zygote64: at void android.view.LayoutInflater.rInflateChildren(org.xmlpull.v1.XmlPullParser, android.view.View, android.util.AttributeSet, boolean) (LayoutInflater.java:831)
2021-08-19 14:48:58.425 8369-8369/com.demo.androidhtml I/zygote64: at android.view.View android.view.LayoutInflater.inflate(org.xmlpull.v1.XmlPullParser, android.view.ViewGroup, boolean) (LayoutInflater.java:515)
2021-08-19 14:48:58.425 8369-8369/com.demo.androidhtml I/zygote64: at android.view.View android.view.LayoutInflater.inflate(int, android.view.ViewGroup, boolean) (LayoutInflater.java:423)
2021-08-19 14:48:58.425 8369-8369/com.demo.androidhtml I/zygote64: at android.view.View android.view.LayoutInflater.inflate(int, android.view.ViewGroup) (LayoutInflater.java:374)
2021-08-19 14:48:58.425 8369-8369/com.demo.androidhtml I/zygote64: at android.view.ViewGroup androidx.appcompat.app.AppCompatDelegateImpl.createSubDecor() (AppCompatDelegateImpl.java:896)
2021-08-19 14:48:58.425 8369-8369/com.demo.androidhtml I/zygote64: at void androidx.appcompat.app.AppCompatDelegateImpl.ensureSubDecor() (AppCompatDelegateImpl.java:806)
2021-08-19 14:48:58.425 8369-8369/com.demo.androidhtml I/zygote64: at void androidx.appcompat.app.AppCompatDelegateImpl.setContentView(int) (AppCompatDelegateImpl.java:693)
2021-08-19 14:48:58.425 8369-8369/com.demo.androidhtml I/zygote64: at void androidx.appcompat.app.AppCompatActivity.setContentView(int) (AppCompatActivity.java:170)
2021-08-19 14:48:58.425 8369-8369/com.demo.androidhtml I/zygote64: at void com.demo.androidhtml.ListActivity.onCreate(android.os.Bundle) (ListActivity.java:31)
2021-08-19 14:48:58.425 8369-8369/com.demo.androidhtml I/zygote64: at void android.app.Activity.performCreate(android.os.Bundle, android.os.PersistableBundle) (Activity.java:7259)
2021-08-19 14:48:58.425 8369-8369/com.demo.androidhtml I/zygote64: at void android.app.Activity.performCreate(android.os.Bundle) (Activity.java:7250)
2021-08-19 14:48:58.425 8369-8369/com.demo.androidhtml I/zygote64: at void android.app.Instrumentation.callActivityOnCreate(android.app.Activity, android.os.Bundle) (Instrumentation.java:1215)
2021-08-19 14:48:58.425 8369-8369/com.demo.androidhtml I/zygote64: at android.app.Activity android.app.ActivityThread.performLaunchActivity(android.app.ActivityThread$ActivityClientRecord, android.content.Intent) (ActivityThread.java:2928)
2021-08-19 14:48:58.425 8369-8369/com.demo.androidhtml I/zygote64: at void android.app.ActivityThread.handleLaunchActivity(android.app.ActivityThread$ActivityClientRecord, android.content.Intent, java.lang.String) (ActivityThread.java:3053)
2021-08-19 14:48:58.425 8369-8369/com.demo.androidhtml I/zygote64: at void android.app.ActivityThread.-wrap11(android.app.ActivityThread, android.app.ActivityThread$ActivityClientRecord, android.content.Intent, java.lang.String) (ActivityThread.java:-1)
2021-08-19 14:48:58.425 8369-8369/com.demo.androidhtml I/zygote64: at void android.app.ActivityThread$H.handleMessage(android.os.Message) (ActivityThread.java:1772)
2021-08-19 14:48:58.425 8369-8369/com.demo.androidhtml I/zygote64: at void android.os.Handler.dispatchMessage(android.os.Message) (Handler.java:106)
2021-08-19 14:48:58.425 8369-8369/com.demo.androidhtml I/zygote64: at void android.os.Looper.loop() (Looper.java:197)
2021-08-19 14:48:58.425 8369-8369/com.demo.androidhtml I/zygote64: at void android.app.ActivityThread.main(java.lang.String[]) (ActivityThread.java:7022)
2021-08-19 14:48:58.425 8369-8369/com.demo.androidhtml I/zygote64: at java.lang.Object java.lang.reflect.Method.invoke(java.lang.Object, java.lang.Object[]) (Method.java:-2)
2021-08-19 14:48:58.425 8369-8369/com.demo.androidhtml I/zygote64: at void com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run() (RuntimeInit.java:515)
2021-08-19 14:48:58.425 8369-8369/com.demo.androidhtml I/zygote64: at void com.android.internal.os.ZygoteInit.main(java.lang.String[]) (ZygoteInit.java:837)
解决办法:
需要修改依赖中的版本为:
implementation 'androidx.appcompat:appcompat:1.3.0-alpha02'
再次运行,异常消失,大功告成!