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

10
我在布局中添加了一个工具栏,现在运行时出现了以下错误:
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>

这是我的应用栏布局:

<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());
        }

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

尝试将嵌套布局放入CoordinatorLayout中,而不是ConstraintLayout。另外,我发现您的嵌套布局缺少约束条件。 - Faisal Naseer
返回翻译文本:同时尝试将androidx.constraintlayout.widget.ConstraintLayout替换为android.support.constraint.ConstraintLayout - Faisal Naseer
请检查您是否已经完成迁移的完整设置。同时,可以参考以下链接:https://developer.android.com/jetpack/androidx/migrate - Faisal Naseer
如果您正在使用Android Studio的“迁移到AndroidX”向导,请查看此答案 - makata
3个回答

39

你需要使用

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

请确保您已添加以下依赖项

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

更新

如果你在你的活动中使用了ToolbarAppBarLayout ,请确保你在你的活动代码中正确导入它们。

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

必须使用 com.google.android.material.appbar.AppBarLayout 和 androidx.appcompat.widget.Toolbar 吗?是否有可能使用支持库的解决方案? - metis
1
如果您正在使用 androidxdependencies,那么您需要使用 <com.google.android.material.appbar.AppBarLayout<androidx.appcompat.widget.Toolbar - AskNilesh

7
@Nilesh的回答对我有用,但还需要更多。对于使用androidx的人们,请在布局中更改以下内容:
android.support.design.widget.CoordinatorLayout
android.support.design.widget.AppBarLayout
android.support.v7.widget.Toolbar
android.support.design.widget.TabLayout
android.support.v4.view.ViewPager

到这里;

androidx.coordinatorlayout.widget.CoordinatorLayout
com.google.android.material.appbar.AppBarLayout
androidx.appcompat.widget.Toolbar
com.google.android.material.tabs.TabLayout
androidx.viewpager.widget.ViewPager

3

除了@NileshRathod的回答之外,关于类充气错误的问题几乎都是由于应用程序级别 build.gradle 文件中的依赖项不正确而导致的。

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

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

我建议您使用MaterialAndroidX依赖项。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接