android.view.InflateException:二进制XML文件第8行:膨胀类android.support.design.widget.AppBarLayout时出错

时间:2018-10-01 16:35:22

标签: xml android-studio android-appbarlayout

我正在尝试运行我的应用程序,然后在我的android项目中不断出错。

  

android.view.InflateException:二进制XML文件第8行:错误   膨胀类android.support.design.widget.AppBarLayout

我见过this。我已经在应用gradle中添加了implementation 'com.android.support:design:27.1.1',但是它不起作用。

这里是错误

10-01 23:04:01.829 12672-12672/mbr.newtest E/AndroidRuntime: FATAL EXCEPTION: main
Process: mbr.newtest, PID: 12672
java.lang.RuntimeException: Unable to start activity ComponentInfo{mbr.newtest/mbr.newtest.MainActivity}: android.view.InflateException: Binary XML file line #8: Error inflating class android.support.design.widget.AppBarLayout
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2315)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2375)
    at android.app.ActivityThread.access$900(ActivityThread.java:147)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1283)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:135)
    at android.app.ActivityThread.main(ActivityThread.java:5253)
    at java.lang.reflect.Method.invoke(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:372)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:913)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:708)
 Caused by: android.view.InflateException: Binary XML file line #8: Error inflating class android.support.design.widget.AppBarLayout
    at android.view.LayoutInflater.createView(LayoutInflater.java:637)
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:747)
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:810)
    at android.view.LayoutInflater.parseInclude(LayoutInflater.java:920)
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:806)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:508)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:418)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:365)
    at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:287)
    at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:139)
    at mbr.newtest.MainActivity.onCreate(MainActivity.java:96)
    at android.app.Activity.performCreate(Activity.java:5982)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2268)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2375) 
    at android.app.ActivityThread.access$900(ActivityThread.java:147) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1283) 
    at android.os.Handler.dispatchMessage(Handler.java:102) 
    at android.os.Looper.loop(Looper.java:135) 
    at android.app.ActivityThread.main(ActivityThread.java:5253) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at java.lang.reflect.Method.invoke(Method.java:372) 
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:913) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:708) 
 Caused by: java.lang.reflect.InvocationTargetException
    at java.lang.reflect.Constructor.newInstance(Native Method)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:288)
    at android.view.LayoutInflater.createView(LayoutInflater.java:611)
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:747) 
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:810) 
    at android.view.LayoutInflater.parseInclude(LayoutInflater.java:920) 
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:806) 
    at android.view.LayoutInflater.inflate(LayoutInflater.java:508) 
    at android.view.LayoutInflater.inflate(LayoutInflater.java:418) 
    at android.view.LayoutInflater.inflate(LayoutInflater.java:365) 
    at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:287) 
    at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:139) 
    at mbr.newtest.MainActivity.onCreate(MainActivity.java:96) 
    at android.app.Activity.performCreate(Activity.java:5982) 
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105) 
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2268) 
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2375) 
    at android.app.ActivityThread.access$900(ActivityThread.java:147) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1283) 
    at android.os.Handler.dispatchMessage(Handler.java:102) 
    at android.os.Looper.loop(Looper.java:135) 
    at android.app.ActivityThread.main(ActivityThread.java:5253) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at java.lang.reflect.Method.invoke(Method.java:372) 
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:913) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:708) 
 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.AppBarLayout.<init>(AppBarLayout.java:158)
    at java.lang.reflect.Constructor.newInstance(Native Method) 
    at java.lang.reflect.Constructor.newInstance(Constructor.java:288) 
    at android.view.LayoutInflater.createView(LayoutInflater.java:611) 
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:747) 
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:810) 
    at android.view.LayoutInflater.parseInclude(LayoutInflater.java:920) 
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:806) 
    at android.view.LayoutInflater.inflate(LayoutInflater.java:508) 
    at android.view.LayoutInflater.inflate(LayoutInflater.java:418) 
    at android.view.LayoutInflater.inflate(LayoutInflater.java:365) 
    at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:287) 
    at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:139) 
    at mbr.newtest.MainActivity.onCreate(MainActivity.java:96) 
    at android.app.Activity.performCreate(Activity.java:5982) 
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105) 
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2268) 
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2375) 
    at android.app.ActivityThread.access$900(ActivityThread.java:147) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1283) 
    at android.os.Handler.dispatchMessage(Handler.java:102) 
    at android.os.Looper.loop(Looper.java:135) 
    at android.app.ActivityThread.main(ActivityThread.java:5253) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at java.lang.reflect.Method.invoke(Method.java:372) 
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:913) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:708)

这是我的XML

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme_AppBarOverlay">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:popupTheme="@style/AppTheme_PopupOverlay" />

    </android.support.design.widget.AppBarLayout>

    <include layout="@layout/content_main" />
</android.support.design.widget.CoordinatorLayout>

1 个答案:

答案 0 :(得分:2)

您需要在设计库中使用Theme.AppCompat主题(或后代)。

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
   ...
</style>

<style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />

<style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />

您需要扩展AppCompatActivity

public class MainActivity extends AppCompatActivity {...}

现在有更多访问版本28.0.0

compile 'com.android.support:appcompat-v7:28.0.0'
compile 'com.android.support:design:28.0.0'