ANDROID:膨胀类android.support.design.widget.AppBarLayout时出错 [英] ANDROID: Error inflating class android.support.design.widget.AppBarLayout

查看:213
本文介绍了ANDROID:膨胀类android.support.design.widget.AppBarLayout时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I added a toolbar to my layout and now I get this error when running:

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()中:

In my activity onCreate():

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

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

I can't figure out why. Any suggestions?

推荐答案

您需要使用

<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,请确保在活动代码中使用了正确的导入

if you have used Toolbar and AppBarLayout inside your activity then make sure you have correct imports inside your activity code

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

这篇关于ANDROID:膨胀类android.support.design.widget.AppBarLayout时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆