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

标签 android android-toolbar android-appbarlayout androidx

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

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

这是我的 Activity 布局:
<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>

这是我的应用栏布局:
<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>

在我的 Activity onCreate() 中:
Toolbar toolbar = findViewById(R.id.toolbar);
        if (toolbar != null) {
            setSupportActionBar(toolbar);
            toolbar.setTitle(getTitle());
        }

我不知道为什么。有什么建议么?

最佳答案

你需要使用

<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'

更新

如果您使用过ToolbarAppBarLayout在您的 Activity 中,然后确保您的 Activity 代码中有正确的导入
import androidx.appcompat.widget.Toolbar
import com.google.android.material.appbar.AppBarLayout

关于ANDROID:错误膨胀类 android.support.design.widget.AppBarLayout,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55298742/

相关文章:

java - 在 Intent.ACTION_OPEN_DOCUMENT_TREE 返回的目录中创建新文件

android - 在 RecyclerView Scroll 上顺利隐藏工具栏?

android - 如何更改 Lollipop 之前和 Lollipop 版本的工具栏菜单项文本颜色

android - 在 Toolbar 和 Tablayout 之间获取高度

java.lang.UnsatisfiedLinkError 有意外的 e_machine : 40

java - Android Proguard - 运行时崩溃(未达到 "onCreate()")

java - Android Fragment生命周期问题

android - 我可以在 android.support.v7.widget.Toolbar 的溢出菜单上使用 actionLayout 吗?

java - 如何使用CoordinatorLayout、Toolbar和fragment在fragment之间切换时出现工具栏

android - 如何将插图应用于内部有图像的 CollapsingToolbarLayout,以便工具栏中的图像绘制在状态栏后面?