ANDROID:错误膨胀类android.support.design.widget.AppBarLayout

问题描述 投票:0回答:2

我在我的布局中添加了一个工具栏,现在运行时出现此错误:

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.myapp/mainactivity.MainActivity}: android.view.InflateException: Binary XML file line #9: Binary XML file line #2: Error inflating class android.support.design.widget.AppBarLayout

这是我的活动布局:

<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <include layout="@layout/appbar"/>

</androidx.constraintlayout.widget.ConstraintLayout>

这是我的appbar布局:

<android.support.design.widget.AppBarLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/app_bar"
    android:theme="@style/Theme.AppCompat.Light.DarkActionBar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        android:elevation="4dp"
        android:theme="@style/ThemeOverlay.AppCompat.ActionBar"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light" >

    </android.support.v7.widget.Toolbar>

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

在我的活动onCreate()中:

Toolbar toolbar = findViewById(R.id.toolbar);
        if (toolbar != null) {
            setSupportActionBar(toolbar);
            toolbar.setTitle(getTitle());
        }

我无法弄清楚为什么。有什么建议?

android android-toolbar android-appbarlayout androidx
2个回答
3
投票

你需要使用

<com.google.android.material.appbar.AppBarLayout
<androidx.appcompat.widget.Toolbar

代替

<android.support.design.widget.AppBarLayout
<android.support.v7.widget.Toolbar

示例代码

<com.google.android.material.appbar.AppBarLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/app_bar"
    android:theme="@style/Theme.AppCompat.Light.DarkActionBar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <androidx.appcompat.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        android:elevation="4dp"
        android:theme="@style/ThemeOverlay.AppCompat.ActionBar"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light" >

    </androidx.appcompat.widget.Toolbar>

</com.google.android.material.appbar.AppBarLayout>

确保你在dependencies下面添加了

implementation 'com.google.android.material:material:1.0.0'

1
投票

进一步添加到@ NileshRathod的答案,关于膨胀类的错误几乎只是由于应用程序级别build.gradle文件中的不正确依赖性而发生。

您必须在应用级别build.gradle文件中添加以下依赖项:

implementation 'com.android.support:design:23.1.0'

好吧,我建议你使用MaterialAndroidX依赖项

© www.soinside.com 2019 - 2024. All rights reserved.