【问题标题】:AppBarLayout cannot be cast to androidx.appcompat.widget.ToolbarAppBarLayout 无法转换为 androidx.appcompat.widget.Toolbar
【发布时间】:2021-02-25 21:16:04
【问题描述】:

我的应用程序在启动时崩溃,这就是我得到的错误 com.google.android.material.appbar.AppBarLayout 无法转换为 androidx.appcompat.widget.Toolbar

这是我的 XML 文件

<LinearLayout
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"
tools:context=".MainActivity"
android:orientation="vertical">

<com.google.android.material.appbar.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/mToolBar">
    <androidx.appcompat.widget.Toolbar
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <ImageView
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:src="@drawable/logo"/>

        <TextView
            android:id="@+id/profileText"
            android:layout_width="match_parent"
            android:layout_height="match_parent"

            android:text="  Hello"
            android:textColor="#000"
            android:textSize="24sp"
            android:textStyle="italic|bold" />
    </androidx.appcompat.widget.Toolbar>

    <com.google.android.material.tabs.TabLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/tabLayout"
        app:tabTextColor="#000"
        app:tabSelectedTextColor="#fff"/>
</com.google.android.material.appbar.AppBarLayout>

<androidx.viewpager.widget.ViewPager
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/viewPager"/>

我被定向到这行代码mToolBar = findViewById(R.id.mToolBar);

我尝试更改 它没有用 idk 有什么解决办法请帮帮我

【问题讨论】:

    标签: android xml material-design


    【解决方案1】:

    如果您想获得对您的Toolbar 的引用,您需要将您的android:id="@+id/mToolBar" 移动到您的Toolbar 并将其从您的AppBarLayout 中删除。

    <com.google.android.material.appbar.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <androidx.appcompat.widget.Toolbar
            android:id="@+id/mToolBar"
            android:layout_width="match_parent"
            android:layout_height="match_parent">
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-11
      • 2021-12-05
      • 2022-01-23
      • 2020-06-06
      • 1970-01-01
      相关资源
      最近更新 更多