最近要维护一个旧项目,接了新的功能,但新功能需要用到新的模块,所以吧啦啦的把旧有的代码都替换为新的Androidx了,原本编译没有报错,心想不太难,但是运行起来,直接挂了。。。。下面是错误的代码:
2019-12-20 15:51:31.670 8729-8729/com.reach.reachplayer E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.reach.reachplayer, PID: 8729
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.reach.reachplayer/com.reach.reachplayer.MainActivity}: android.view.InflateException: Binary XML file line #2: Binary XML file line #2: Error inflating class android.support.design.widget.CoordinatorLayout
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3194)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3302)
at android.app.ActivityThread.-wrap12(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1891)
at android.os.Handler.dispatchMessage(Handler.java:108)
at android.os.Looper.loop(Looper.java:166)
at android.app.ActivityThread.main(ActivityThread.java:7425)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:245)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:921)
Caused by: android.view.InflateException: Binary XML file line #2: Binary XML file line #2: Error inflating class android.support.design.widget.CoordinatorLayout
Caused by: android.view.InflateException: Binary XML file line #2: Error inflating class android.support.design.widget.CoordinatorLayout
Caused by: java.lang.ClassNotFoundException: Didn't find class "android.support.design.widget.CoordinatorLayout" on path: DexPathList[[zip file "/data/app/com.reach.reachplayer-QQnkS0v4W79B6CnLlq0a-Q==/base.apk"],nativeLibraryDirectories=[/data/app/com.reach.reachplayer-QQnkS0v4W79B6CnLlq0a-Q==/lib/arm, /data/app/com.reach.reachplayer-QQnkS0v4W79B6CnLlq0a-Q==/base.apk!/lib/armeabi-v7a, /system/lib, /vendor/lib, /product/lib]]
at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:93)
at java.lang.ClassLoader.loadClass(ClassLoader.java:379)
at java.lang.ClassLoader.loadClass(ClassLoader.java:312)
at android.view.LayoutInflater.createView(LayoutInflater.java:613)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:801)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:741)
at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
at android.view.LayoutInflater.inflate(LayoutInflater.java:374)
at com.android.internal.policy.PhoneWindow.setContentView(PhoneWindow.java:477)
at com.android.internal.policy.HwPhoneWindow.setContentView(HwPhoneWindow.java:321)
at android.app.Activity.setContentView(Activity.java:2839)
at com.reach.reachplayer.BaseActivity.setContentView(BaseActivity.java:20)
at com.reach.reachplayer.MainActivity.onCreate(MainActivity.java:74)
at android.app.Activity.performCreate(Activity.java:7372)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1218)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3147)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3302)
at android.app.ActivityThread.-wrap12(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1891)
at android.os.Handler.dispatchMessage(Handler.java:108)
at android.os.Looper.loop(Looper.java:166)
at android.app.ActivityThread.main(ActivityThread.java:7425)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:245)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:921)
在网上找了很多方法,概括起来就是两种方法,一是AndroidManifest里的主题,另一种就是在build.gradle里添加新的依赖,但都是没有作用。
后来想到是替换了新的androidx,那既然这样,可以把红框里的替换为androidx:
替换androidx的一段代码如下面所示:
androidx.coordinatorlayout.widget.CoordinatorLayout
替换后的截图:
后记,替换为androidx后,有很多地方都出错了,报的都是和这个类似的错误,如下所示:
所以解决办法都是把报错的xml文件替换为androidx即可~