Android Studio3.0添加com.android.support:design:包的CoordinatorLayout后出现无法预览/闪退问题的解决方法

时间:2024-05-23 07:50:57

报错日志:

02-15 17:54:54.969 1895-1895/? E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.coordinatorlayoutdemo, PID: 1895
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.coordinatorlayoutdemo/com.example.coordinatorlayoutdemo.MainActivity}: android.view.InflateException: Binary XML file line #0: Binary XML file line #0: Error inflating class android.support.design.widget.CoordinatorLayout
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2856)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2931)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThreadH.handleMessage(ActivityThread.java:1620)atandroid.os.Handler.dispatchMessage(Handler.java:105)atandroid.os.Looper.loop(Looper.java:173)atandroid.app.ActivityThread.main(ActivityThread.java:6698)atjava.lang.reflect.Method.invoke(NativeMethod)atcom.android.internal.os.ZygoteH.handleMessage(ActivityThread.java:1620) at android.os.Handler.dispatchMessage(Handler.java:105) at android.os.Looper.loop(Looper.java:173) at android.app.ActivityThread.main(ActivityThread.java:6698) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteMethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:782)
Caused by: android.view.InflateException: Binary XML file line #0: Binary XML file line #0: Error inflating class android.support.design.widget.CoordinatorLayout
Caused by: android.view.InflateException: Binary XML file line #0: Error inflating class android.support.design.widget.CoordinatorLayout
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Constructor.newInstance0(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:334)
at android.view.LayoutInflater.createView(LayoutInflater.java:651)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:794)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:734)
at android.view.LayoutInflater.inflate(LayoutInflater.java:496)
at android.view.LayoutInflater.inflate(LayoutInflater.java:427)
at android.view.LayoutInflater.inflate(LayoutInflater.java:374)
at com.android.internal.policy.PhoneWindow.setContentView(PhoneWindow.java:418)
at android.app.Activity.setContentView(Activity.java:2708)
at com.example.coordinatorlayoutdemo.MainActivity.onCreate(MainActivity.java:13)
at android.app.Activity.performCreate(Activity.java:7040)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1214)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2809)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2931)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThreadH.handleMessage(ActivityThread.java:1620)atandroid.os.Handler.dispatchMessage(Handler.java:105)atandroid.os.Looper.loop(Looper.java:173)atandroid.app.ActivityThread.main(ActivityThread.java:6698)atjava.lang.reflect.Method.invoke(NativeMethod)atcom.android.internal.os.ZygoteH.handleMessage(ActivityThread.java:1620) at android.os.Handler.dispatchMessage(Handler.java:105) at android.os.Looper.loop(Looper.java:173) at android.app.ActivityThread.main(ActivityThread.java:6698) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteMethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:782)
Caused by: java.lang.IllegalArgumentException: You need to use a Theme.AppCompat theme (or descendant) with the design library.
at android.support.design.widget.ThemeUtils.checkAppCompatTheme(ThemeUtils.java:33)
at android.support.design.widget.CoordinatorLayout.(CoordinatorLayout.java:206)
at android.support.design.widget.CoordinatorLayout.(CoordinatorLayout.java:200)
at java.lang.reflect.Constructor.newInstance0(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:334)
at android.view.LayoutInflater.createView(LayoutInflater.java:651)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:794)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:734)
at android.view.LayoutInflater.inflate(LayoutInflater.java:496)
at android.view.LayoutInflater.inflate(LayoutInflater.java:427)
at android.view.LayoutInflater.inflate(LayoutInflater.java:374)
at com.android.internal.policy.PhoneWindow.setContentView(PhoneWindow.java:418)
at android.app.Activity.setContentView(Activity.java:2708)
at com.example.coordinatorlayoutdemo.MainActivity.onCreate(MainActivity.java:13)
at android.app.Activity.performCreate(Activity.java:7040)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1214)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2809)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2931)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThreadH.handleMessage(ActivityThread.java:1620)atandroid.os.Handler.dispatchMessage(Handler.java:105)atandroid.os.Looper.loop(Looper.java:173)atandroid.app.ActivityThread.main(ActivityThread.java:6698)atjava.lang.reflect.Method.invoke(NativeMethod)atcom.android.internal.os.ZygoteH.handleMessage(ActivityThread.java:1620) at android.os.Handler.dispatchMessage(Handler.java:105) at android.os.Looper.loop(Looper.java:173) at android.app.ActivityThread.main(ActivityThread.java:6698) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteMethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:782)

按网上的说法,要使用CoordinatorLayout,应该直接在build.gradle中添加依赖 compile 'com.android.support:design:26.1.0' 便可:

Android Studio3.0添加com.android.support:design:包的CoordinatorLayout后出现无法预览/闪退问题的解决方法

可是添加后在布局文件中引用时,却出现无法预览的问题:

Android Studio3.0添加com.android.support:design:包的CoordinatorLayout后出现无法预览/闪退问题的解决方法

后来百度了一下,说是要修改主题为AppTheme中的其中一种,但是默认不就是AppTheme吗,好吧,改:

Android Studio3.0添加com.android.support:design:包的CoordinatorLayout后出现无法预览/闪退问题的解决方法

改完之后还是不行,预览没问题了,但是编译会闪退报错,即报了开头日志的错误,又搜了半天,发现要在AndroidManifest.xml中进行修改才行,直接将主题替换为:android:theme="@style/Theme.AppCompat.Light.NoActionBar",然后build一下项目或者重启就可以了。要用design包下的东西,基本上都要改这个。

Android Studio3.0添加com.android.support:design:包的CoordinatorLayout后出现无法预览/闪退问题的解决方法

好了,这样问题就解决了,搜了好久搜不到,特此记录一下

鸣谢:https://blog.****.net/u014078990/article/details/71404999