android - AppBarLayout 无法转换为 androidx.appcompat.widget.Toolbar
问题描述
我的应用程序在启动时崩溃,这是我得到的错误 com.google.android.material.appbar.AppBarLayout cannot be cast to 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);
我试图将<androidx.appcompat.widget.Toolbar更改 为<android.widget.Toolbar 并且它没有工作 idk 有什么解决方案请帮帮我
解决方案
如果您想获得对您的引用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">
推荐阅读
- javascript - 反应函数的良好实践
- r - 将一年的密度平均值和标准差绘制为 R 中的条形图
- sas - 根据公共数据字段比较不同行之间的开始/结束日期
- c++ - 在 C++ 中,最好不要对使用 RTTI 的函数参数使用按值传递?
- c - 结构是巨大的。为什么?
- angularjs - 有没有办法使用角度材料拖放来调整大小
- python - Google App Engine:禁用服务的会话中间件
- amazon-web-services - Jenkins 启动 AWS EC2 实例的工作?
- c# - C# 正则表达式在字符串匹配后查找并删除文本
- python - 在linux中打开文件时出现UnicodeDecodeError,但在Windows中打开相同的文件