android.widget.Toolbar 无法转换为 androidx.appcompat.widget.Toolbar(android 工具栏)

Posted

技术标签:

【中文标题】android.widget.Toolbar 无法转换为 androidx.appcompat.widget.Toolbar(android 工具栏)【英文标题】:android.widget.Toolbar cannot be cast to androidx.appcompat.widget.Toolbar (android toolbar) 【发布时间】:2022-01-11 15:55:24 【问题描述】:

MainActivity.kt

package com.sitamadex11.gcectapp

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.MenuItem
import android.view.View
import android.widget.Toast
import androidx.appcompat.widget.Toolbar
import androidx.appcompat.app.ActionBarDrawerToggle
import androidx.drawerlayout.widget.DrawerLayout
import com.google.android.material.navigation.NavigationView


class MainActivity : AppCompatActivity() 

    lateinit var toggle: ActionBarDrawerToggle

    override fun onCreate(savedInstanceState: Bundle?) 
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val toolbar: Toolbar = findViewById(R.id.toolbar)
        setSupportActionBar(toolbar)


        val drawerLayout=findViewById<View>(R.id.drawerLayout) as DrawerLayout

        toggle = ActionBarDrawerToggle(this,drawerLayout ,toolbar,R.string.open,R.string.close)
        drawerLayout.addDrawerListener(toggle)
        toggle.syncState()

        supportActionBar?.setDisplayHomeAsUpEnabled(true)

        val navView=findViewById<View>(R.id.navView) as NavigationView

        navView.setNavigationItemSelectedListener
            when(it.itemId)
                R.id.nav_home -> Toast.makeText(applicationContext,
                    "Clicked Home",Toast.LENGTH_SHORT).show()
                R.id.nav_aboutus -> Toast.makeText(applicationContext,
                    "Clicked About Us",Toast.LENGTH_SHORT).show()
                R.id.nav_acad -> Toast.makeText(applicationContext,
                    "Clicked Academics",Toast.LENGTH_SHORT).show()

            
            true
        

    

    override fun onOptionsItemSelected(item: MenuItem): Boolean 

        if(toggle.onOptionsItemSelected(item))
            return true
        
        return super.onOptionsItemSelected(item)
    

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.widget.DrawerLayout 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:id="@+id/drawerLayout"
    android:layout_
    android:layout_
    tools:context=".MainActivity">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_
        android:layout_>

        <Toolbar

            android:layout_
            android:layout_
            android:id="@+id/toolbar"
            android:title="GCECT"
            android:background="@color/teal_200"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            />

        <TextView
            android:layout_
            android:layout_
            android:text="ACTIVITY"
            android:textSize="40sp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.5"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

    </androidx.constraintlayout.widget.ConstraintLayout>

    <com.google.android.material.navigation.NavigationView
        android:id="@+id/navView"
        android:layout_
        android:layout_
        app:headerLayout="@layout/nav_header"
        app:menu="@menu/nav_drawer_menu"
        android:layout_gravity="start"
        android:fitsSystemWindows="true"/>

</androidx.drawerlayout.widget.DrawerLayout>

logcat 错误信息:


2021-12-06 17:41:58.372 3377-3377/com.sitamadex11.gcectapp E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.sitamadex11.gcectapp, PID: 3377
    java.lang.RuntimeException:**Unable to start activity ComponentInfocom.sitamadex11.gcectapp/com.sitamadex11.gcectapp.MainActivity: java.lang.ClassCastException: android.widget.Toolbar cannot be cast to androidx.appcompat.widget.Toolbar**
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3449)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3601)
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85)
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2066)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:223)
        at android.app.ActivityThread.main(ActivityThread.java:7656)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
     Caused by: java.lang.ClassCastException: android.widget.Toolbar cannot be cast to androidx.appcompat.widget.Toolbar
        at com.sitamadex11.gcectapp.MainActivity.onCreate(MainActivity.kt:23)
        at android.app.Activity.performCreate(Activity.java:7994)
        at android.app.Activity.performCreate(Activity.java:7978)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1309)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3422)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3601) 
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85) 
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135) 
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2066) 
        at android.os.Handler.dispatchMessage(Handler.java:106) 
        at android.os.Looper.loop(Looper.java:223) 
        at android.app.ActivityThread.main(ActivityThread.java:7656) 

如何解决这个问题?

【问题讨论】:

这能回答你的问题吗? android.widget.Toolbar cannot be cast to androidx.appcompat.widget.Toolbar even though that is not the case 【参考方案1】:

发生此错误是因为您的 XML 使用的是基础 android Toolbar,但您的代码正在导入 AndroidX Toolbar

您可以按如下方式更改您的 xml:

<androidx.appcompat.widget.Toolbar

代替:

<Toolbar

【讨论】:

感谢您指出这一点。问题解决了

以上是关于android.widget.Toolbar 无法转换为 androidx.appcompat.widget.Toolbar(android 工具栏)的主要内容,如果未能解决你的问题,请参考以下文章

移除 android.widget.Toolbar 阴影

support v7兼容包的用途(三 、v7兼容包和系统自带的比较和注意点)

无法加载数据,无法支付

无法推送到 Heroku '致命:无法访问..无法解析主机:(无);提供节点名或服务名,或未知'

无法写入内容:无法延迟初始化角色集合,无法初始化代理 - 无会话

chrome和ie无法打开网页,也无法连接WIFI