创建一个Activity导致错误膨胀类android.support.design.widget.CoordinatorLayout

时间:2017-05-29 20:28:03

标签: android xml

该应用程序工作正常,然后我添加了一个新的Activity,它开始给我这个错误。我已经尝试将主题设置为app compat主题并使用app compat活动。仍然没有任何效果。

Process: info.androidhive.loginandregistration, PID: 28764
    java.lang.RuntimeException: Unable to start activity ComponentInfo{info.androidhive.loginandregistration/info.androidhive.loginandregistration.activity.MainScreenActivity}: android.view.InflateException: Binary XML file line #11: Binary XML file line #2: Error inflating class android.support.design.widget.CoordinatorLayout
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2646)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)
    at android.app.ActivityThread.-wrap12(ActivityThread.java)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:154)
    at android.app.ActivityThread.main(ActivityThread.java:6077)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
    Caused by: android.view.InflateException: Binary XML file line #11: 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.reflect.InvocationTargetException
    at java.lang.reflect.Constructor.newInstance0(Native Method)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:430)
    at android.view.LayoutInflater.createView(LayoutInflater.java:645)
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:787)
    at android.view.LayoutInflater.parseInclude(LayoutInflater.java:964)
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:854)
    at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:821)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:518)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:426)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:377)
    at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:280)
    at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140)
    at info.androidhive.loginandregistration.activity.MainScreenActivity.onCreate(MainScreenActivity.java:69)
    at android.app.Activity.performCreate(Activity.java:6664)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2599)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)
    at android.app.ActivityThread.-wrap12(ActivityThread.java)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:154)
    at android.app.ActivityThread.main(ActivityThread.java:6077)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
    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:36)
    at android.support.design.widget.CoordinatorLayout.<init>(CoordinatorLayout.java:185)
    at android.support.design.widget.CoordinatorLayout.<init>(CoordinatorLayout.java:179)
    at java.lang.reflect.Constructor.newInstance0(Native Method) 
    at java.lang.reflect.Constructor.newInstance(Constructor.java:430) 
    at android.view.LayoutInflater.createView(LayoutInflater.java:645) 
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:787) 
    at android.view.LayoutInflater.parseInclude(LayoutInflater.java:964) 
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:854) 
    at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:821) 
    at android.view.LayoutInflater.inflate(LayoutInflater.java:518) 
    at android.view.LayoutInflater.inflate(LayoutInflater.java:426) 
    at android.view.LayoutInflater.inflate(LayoutInflater.java:377) 
    at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:280) 
    at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140) 
    at info.androidhive.loginandregistration.activity.MainScreenActivity.onCreate(MainScreenActivity.java:69) 
    at android.app.Activity.performCreate(Activity.java:6664) 
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118) 
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2599) 
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707) 
    at android.app.ActivityThread.-wrap12(ActivityThread.java) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460) 
    at android.os.Handler.dispatchMessage(Handler.java:102) 
    at android.os.Looper.loop(Looper.java:154) 
    at android.app.ActivityThread.main(ActivityThread.java:6077) 

1 个答案:

答案 0 :(得分:1)

java.lang.IllegalArgumentException: You need to use a Theme.AppCompat theme (or descendant) with the design library

我的猜测是:

  • 首次创建应用程序时,如果您在新项目向导中创建了第一个活动,则取消选中&#34;向后兼容(AppCompat)&#34;复选框

  • 当您添加新活动时,您是通过新活动向导完成的,这次您选中了复选框

您的整体应用主题不会使用Theme.AppCompat,因为您通过取消选中该复选框选择退出appcompat-v7。您的新活动正在扩展AppCompatActivity,但它仍在使用应用程序主题,这是一个会产生错误的冲突。

从策略上讲,更改新MainScreenActivity以使其从Activity延伸,而不是AppCompatActivity。您可能需要进行一些其他调整,具体取决于您选择的活动模板及其创建的资源(例如,将app:更改为android:菜单资源中的属性。)

从策略上讲,您需要决定是否要使用appcompat-v7,然后坚持使用该选项来完成此项目。使用appcompat-v7可以让您在Android版本(返回API级别14)中获得一致的外观,代价是您的应用程序大约增加1MB,并且在编写应用程序时会增加一些额外的复杂性。