Java android studio 应用程序的工具栏错误

Toolbar error on Java android studio application

我正在尝试在我的主页 activity 的应用程序中添加一个工具栏。我有以下问题。 error: incompatible types: android.widget.Toolbar cannot be converted to androidx.appcompat.widget.Toolbar.。我有红色下划线的“工具栏”。

screenshot of the error

知道会是什么吗?我只发现它可能是一个导入问题,但我已经尝试更改它但没有成功。

谢谢。

这是我的 XML 代码:

<ScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <androidx.appcompat.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:background="@color/colorAccent"
        android:minHeight="?actionBarTheme"
        android:theme="?actionBarTheme"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:navigationIcon="@drawable/menubuttonofthreelines_79781">

    </androidx.appcompat.widget.Toolbar>

</ScrollView>

这是我的 HomeActivity 代码:

toolbar = findViewById(R.id.toolbar);

setSupportActionBar(toolbar);
ActionBarDrawerToggle toogle = new ActionBarDrawerToggle(this,drawerLayout,toolbar,
                  R.string.navigation_drawer_open,R.string.navigation_drawer_close);
drawerLayout.addDrawerListener(toogle);
toogle.syncState();

在 java activity 文件中从 android.widget 包中删除工具栏

import  android.widget.Toolbar;

并改为从 androidx.appcompat.widget 包中添加 androidx 工具栏

import androidx.appcompat.widget.Toolbar;

尝试将项目迁移到 AndroidX。您正在使用来自 androidx 的工具栏,但您已通过 android.widget.

形式传递给 setSupportActionBar 工具栏

https://developer.android.com/jetpack/androidx/migrate