错误日志:
2020-09-25 16:47:04.372 18356-18356/com.example.shilintestdemo I/zygote64: Caused by: java.lang.ClassNotFoundException: Didn't find class "android.view.View$OnUnhandledKeyEventListener" on path: DexPathList[[zip file "/data/app/com.example.shilintestdemo-Kl3uXf7Q1gNjPDFCzYIzrw==/base.apk"],nativeLibraryDirectories=[/data/app/com.example.shilintestdemo-Kl3uXf7Q1gNjPDFCzYIzrw==/lib/arm64, /system/lib64, /system/vendor/lib64]]
2020-09-25 16:47:04.372 18356-18356/com.example.shilintestdemo I/zygote64: at java.lang.Class dalvik.system.BaseDexClassLoader.findClass(java.lang.String) (BaseDexClassLoader.java:125)
2020-09-25 16:47:04.372 18356-18356/com.example.shilintestdemo I/zygote64: at java.lang.Class java.lang.ClassLoader.loadClass(java.lang.String, boolean) (ClassLoader.java:379)
2020-09-25 16:47:04.372 18356-18356/com.example.shilintestdemo I/zygote64: at java.lang.Class java.lang.ClassLoader.loadClass(java.lang.String) (ClassLoader.java:312)
2020-09-25 16:47:04.372 18356-18356/com.example.shilintestdemo I/zygote64: at void androidx.core.view.ViewCompat.setBackground(android.view.View, android.graphics.drawable.Drawable) (ViewCompat.java:2678)
2020-09-25 16:47:04.372 18356-18356/com.example.shilintestdemo I/zygote64: at void androidx.appcompat.widget.ActionBarContainer.<init>(android.content.Context, android.util.AttributeSet) (ActionBarContainer.java:63)
2020-09-25 16:47:04.372 18356-18356/com.example.shilintestdemo I/zygote64: at java.lang.Object java.lang.reflect.Constructor.newInstance0(java.lang.Object[]) (Constructor.java:-2)
2020-09-25 16:47:04.373 18356-18356/com.example.shilintestdemo I/zygote64: at java.lang.Object java.lang.reflect.Constructor.newInstance(java.lang.Object[]) (Constructor.java:334)
2020-09-25 16:47:04.373 18356-18356/com.example.shilintestdemo I/zygote64: at android.view.View android.view.LayoutInflater.createView(java.lang.String, java.lang.String, android.util.AttributeSet) (LayoutInflater.java:651)
2020-09-25 16:47:04.373 18356-18356/com.example.shilintestdemo 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:794)
2020-09-25 16:47:04.373 18356-18356/com.example.shilintestdemo I/zygote64: at android.view.View android.view.LayoutInflater.createViewFromTag(android.view.View, java.lang.String, android.content.Context, android.util.AttributeSet) (LayoutInflater.java:734)
2020-09-25 16:47:04.373 18356-18356/com.example.shilintestdemo I/zygote64: at void android.view.LayoutInflater.rInflate(org.xmlpull.v1.XmlPullParser, android.view.View, android.content.Context, android.util.AttributeSet, boolean) (LayoutInflater.java:867)
2020-09-25 16:47:04.373 18356-18356/com.example.shilintestdemo I/zygote64: at void android.view.LayoutInflater.rInflateChildren(org.xmlpull.v1.XmlPullParser, android.view.View, android.util.AttributeSet, boolean) (LayoutInflater.java:828)
2020-09-25 16:47:04.373 18356-18356/com.example.shilintestdemo I/zygote64: at android.view.View android.view.LayoutInflater.inflate(org.xmlpull.v1.XmlPullParser, android.view.ViewGroup, boolean) (LayoutInflater.java:519)
2020-09-25 16:47:04.373 18356-18356/com.example.shilintestdemo I/zygote64: at android.view.View android.view.LayoutInflater.inflate(int, android.view.ViewGroup, boolean) (LayoutInflater.java:427)
2020-09-25 16:47:04.373 18356-18356/com.example.shilintestdemo I/zygote64: at android.view.View android.view.LayoutInflater.inflate(int, android.view.ViewGroup) (LayoutInflater.java:374)
2020-09-25 16:47:04.373 18356-18356/com.example.shilintestdemo I/zygote64: at android.view.ViewGroup androidx.appcompat.app.AppCompatDelegateImpl.createSubDecor() (AppCompatDelegateImpl.java:896)
2020-09-25 16:47:04.373 18356-18356/com.example.shilintestdemo I/zygote64: at void androidx.appcompat.app.AppCompatDelegateImpl.ensureSubDecor() (AppCompatDelegateImpl.java:806)
2020-09-25 16:47:04.373 18356-18356/com.example.shilintestdemo I/zygote64: at void androidx.appcompat.app.AppCompatDelegateImpl.setContentView(int) (AppCompatDelegateImpl.java:693)
2020-09-25 16:47:04.373 18356-18356/com.example.shilintestdemo I/zygote64: at void androidx.appcompat.app.AppCompatActivity.setContentView(int) (AppCompatActivity.java:170)
2020-09-25 16:47:04.373 18356-18356/com.example.shilintestdemo I/zygote64: at void com.example.shilintestdemo.badge.BadgeActivity.onCreate(android.os.Bundle) (BadgeActivity.java:37)
2020-09-25 16:47:04.373 18356-18356/com.example.shilintestdemo I/zygote64: at void android.app.Activity.performCreate(android.os.Bundle, android.os.PersistableBundle) (Activity.java:7088)
2020-09-25 16:47:04.373 18356-18356/com.example.shilintestdemo I/zygote64: at void android.app.Activity.performCreate(android.os.Bundle) (Activity.java:7079)
2020-09-25 16:47:04.373 18356-18356/com.example.shilintestdemo I/zygote64: at void android.app.Instrumentation.callActivityOnCreate(android.app.Activity, android.os.Bundle) (Instrumentation.java:1215)
2020-09-25 16:47:04.373 18356-18356/com.example.shilintestdemo I/zygote64: at android.app.Activity android.app.ActivityThread.performLaunchActivity(android.app.ActivityThread$ActivityClientRecord, android.content.Intent) (ActivityThread.java:2770)
2020-09-25 16:47:04.373 18356-18356/com.example.shilintestdemo I/zygote64: at void android.app.ActivityThread.handleLaunchActivity(android.app.ActivityThread$ActivityClientRecord, android.content.Intent, java.lang.String) (ActivityThread.java:2895)
2020-09-25 16:47:04.373 18356-18356/com.example.shilintestdemo I/zygote64: at void android.app.ActivityThread.-wrap11(android.app.ActivityThread, android.app.ActivityThread$ActivityClientRecord, android.content.Intent, java.lang.String) (ActivityThread.java:-1)
2020-09-25 16:47:04.373 18356-18356/com.example.shilintestdemo I/zygote64: at void android.app.ActivityThread$H.handleMessage(android.os.Message) (ActivityThread.java:1616)
2020-09-25 16:47:04.373 18356-18356/com.example.shilintestdemo I/zygote64: at void android.os.Handler.dispatchMessage(android.os.Message) (Handler.java:106)
2020-09-25 16:47:04.373 18356-18356/com.example.shilintestdemo I/zygote64: at void android.os.Looper.loop() (Looper.java:176)
2020-09-25 16:47:04.373 18356-18356/com.example.shilintestdemo I/zygote64: at void android.app.ActivityThread.main(java.lang.String[]) (ActivityThread.java:6651)
2020-09-25 16:47:04.373 18356-18356/com.example.shilintestdemo I/zygote64: at java.lang.Object java.lang.reflect.Method.invoke(java.lang.Object, java.lang.Object[]) (Method.java:-2)
2020-09-25 16:47:04.373 18356-18356/com.example.shilintestdemo I/zygote64: at void com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run() (RuntimeInit.java:547)
2020-09-25 16:47:04.373 18356-18356/com.example.shilintestdemo I/zygote64: at void com.android.internal.os.ZygoteInit.main(java.lang.String[]) (ZygoteInit.java:824)
问题:在一个新建项目直接跑setContentView(getLayoutResId) 布局出现的错误,但是程序能正常跑。查了一下主要是(低于29)编译版本跟运行机器版本不一致才会出现。
解决:添加依赖
implementation 'androidx.appcompat:appcompat:1.3.0-alpha02'