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

报错日志:

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.ActivityThread H . h a n d l e M e s s a g e ( A c t i v i t y T h r e a d . j a v a : 1620 ) a t a n d r o i d . o s . H a n d l e r . d i s p a t c h M e s s a g e ( H a n d l e r . j a v a : 105 ) a t a n d r o i d . o s . L o o p e r . l o o p ( L o o p e r . j a v a : 173 ) a t a n d r o i d . a p p . A c t i v i t y T h r e a d . m a i n ( A c t i v i t y T h r e a d . j a v a : 6698 ) a t j a v a . l a n g . r e f l e c t . M e t h o d . i n v o k e ( N a t i v e M e t h o d ) a t c o m . a n d r o i d . i n t e r n a l . o s . Z y g o t e H.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.Zygote H.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.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.ActivityThread H . h a n d l e M e s s a g e ( A c t i v i t y T h r e a d . j a v a : 1620 ) a t a n d r o i d . o s . H a n d l e r . d i s p a t c h M e s s a g e ( H a n d l e r . j a v a : 105 ) a t a n d r o i d . o s . L o o p e r . l o o p ( L o o p e r . j a v a : 173 ) a t a n d r o i d . a p p . A c t i v i t y T h r e a d . m a i n ( A c t i v i t y T h r e a d . j a v a : 6698 ) a t j a v a . l a n g . r e f l e c t . M e t h o d . i n v o k e ( N a t i v e M e t h o d ) a t c o m . a n d r o i d . i n t e r n a l . o s . Z y g o t e H.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.Zygote H.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.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.ActivityThread H . h a n d l e M e s s a g e ( A c t i v i t y T h r e a d . j a v a : 1620 ) a t a n d r o i d . o s . H a n d l e r . d i s p a t c h M e s s a g e ( H a n d l e r . j a v a : 105 ) a t a n d r o i d . o s . L o o p e r . l o o p ( L o o p e r . j a v a : 173 ) a t a n d r o i d . a p p . A c t i v i t y T h r e a d . m a i n ( A c t i v i t y T h r e a d . j a v a : 6698 ) a t j a v a . l a n g . r e f l e c t . M e t h o d . i n v o k e ( N a t i v e M e t h o d ) a t c o m . a n d r o i d . i n t e r n a l . o s . Z y g o t e H.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.Zygote H.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.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后出现无法预览/闪退问题的解决方法_第1张图片

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

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

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

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

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

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

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

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

你可能感兴趣的:(Android,Studio)