我的自定义抽屉导航工具栏有什么错误?

What is the error in my custom drawer navigation toolbar?

当我尝试 link 使用 ActionBarDrawerToggle 自定义工具栏时,我的应用程序崩溃了。以下是我的代码:

package com.example.customnavdrawerapp;

import androidx.annotation.NonNull;
import androidx.appcompat.app.ActionBarDrawerToggle;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.view.GravityCompat;
import androidx.drawerlayout.widget.DrawerLayout;

import android.app.TaskStackBuilder;
import android.os.Bundle;
import android.view.Gravity;
import android.view.MenuItem;
import android.widget.Toast;
import android.widget.Toolbar;

import com.google.android.material.navigation.NavigationView;

public class MainActivity extends AppCompatActivity {

    NavigationView navView;
    ActionBarDrawerToggle toggle;
    DrawerLayout drawerLayout;
    androidx.appcompat.widget.Toolbar toolbarr; //THIS AS SOON AS DEFINED WITH ID SHOWS ERROR

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        toolbarr = findViewById(R.id.toolbar); //REMOVE THIS LINE AND THE APP WORKS PERFECT, BUT DOESN'T OPEN THE NAVIGATION VIEW ON CLICKING THE TOOLBAR


        navView = findViewById(R.id.navMenu);

        drawerLayout = findViewById(R.id.drawer);

        toggle= new ActionBarDrawerToggle(this,drawerLayout,toolbarr,R.string.open,R.string.close);

        drawerLayout.addDrawerListener(toggle);

        toggle.syncState();

        navView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item)
            {
                switch (item.getItemId())
                {
                    case R.id.menu_home:
                        Toast.makeText(getApplicationContext(),"Home panel is open",Toast.LENGTH_LONG).show();
                        drawerLayout.closeDrawer(GravityCompat.START);
                        break;
                    case R.id.menu_call:
                        Toast.makeText(getApplicationContext(),"Call panel is open",Toast.LENGTH_LONG).show();
                        drawerLayout.closeDrawer(GravityCompat.START);
                        break;
                    case R.id.menu_setting:
                        Toast.makeText(getApplicationContext(),"Setting panel is open",Toast.LENGTH_LONG).show();
                        drawerLayout.closeDrawer(GravityCompat.START);
                        break;

                }

                return true;
            }
        });

    }
}

我在代码中添加了注释以说明导致错误的原因。

如果我定义工具栏,应用程序会一直停止。

堆栈跟踪:

2021-05-26 06:33:55.811 24315-24315/? I/tomnavdrawerap: Late-enabling -Xcheck:jni
2021-05-26 06:33:55.862 24315-24315/? E/tomnavdrawerap: Unknown bits set in runtime_flags: 0x8000
2021-05-26 06:33:55.907 24315-24315/? D/ActivityThread: setConscryptValidator
2021-05-26 06:33:55.908 24315-24315/? D/ActivityThread: setConscryptValidator - put
2021-05-26 06:33:56.334 24315-24315/com.example.customnavdrawerapp I/Perf: Connecting to perf service.
2021-05-26 06:33:56.573 24315-24315/com.example.customnavdrawerapp D/PhoneWindow: forceLight changed to true [] from com.android.internal.policy.PhoneWindow.updateForceLightNavigationBar:4238 com.android.internal.policy.DecorView.updateColorViews:1510 com.android.internal.policy.PhoneWindow.dispatchWindowAttributesChanged:3216 android.view.Window.setFlags:1148 com.android.internal.policy.PhoneWindow.generateLayout:2444 
2021-05-26 06:33:56.576 24315-24315/com.example.customnavdrawerapp I/MultiWindowDecorSupport: updateCaptionType >> DecorView@3f6abf6[], isFloating: false, isApplication: true, hasWindowDecorCaption: false, hasWindowControllerCallback: true
2021-05-26 06:33:56.576 24315-24315/com.example.customnavdrawerapp D/MultiWindowDecorSupport: setCaptionType = 0, DecorView = DecorView@3f6abf6[]
2021-05-26 06:33:56.656 24315-24315/com.example.customnavdrawerapp W/tomnavdrawerap: Accessing hidden method Landroid/view/View;->computeFitSystemWindows(Landroid/graphics/Rect;Landroid/graphics/Rect;)Z (greylist, reflection, allowed)
2021-05-26 06:33:56.659 24315-24315/com.example.customnavdrawerapp W/tomnavdrawerap: Accessing hidden method Landroid/view/ViewGroup;->makeOptionalFitsSystemWindows()V (greylist, reflection, allowed)
2021-05-26 06:33:56.938 24315-24315/com.example.customnavdrawerapp D/AndroidRuntime: Shutting down VM
2021-05-26 06:33:56.943 24315-24315/com.example.customnavdrawerapp E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.customnavdrawerapp, PID: 24315
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.customnavdrawerapp/com.example.customnavdrawerapp.MainActivity}: java.lang.ClassCastException: android.widget.Toolbar cannot be cast to androidx.appcompat.widget.Toolbar
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3556)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3703)
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)
        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:2216)
        at android.os.Handler.dispatchMessage(Handler.java:107)
        at android.os.Looper.loop(Looper.java:237)
        at android.app.ActivityThread.main(ActivityThread.java:7948)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1075)
     Caused by: java.lang.ClassCastException: android.widget.Toolbar cannot be cast to androidx.appcompat.widget.Toolbar
        at com.example.customnavdrawerapp.MainActivity.onCreate(MainActivity.java:30)
        at android.app.Activity.performCreate(Activity.java:7955)
        at android.app.Activity.performCreate(Activity.java:7944)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1307)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3531)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3703) 
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83) 
        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:2216) 
        at android.os.Handler.dispatchMessage(Handler.java:107) 
        at android.os.Looper.loop(Looper.java:237) 
        at android.app.ActivityThread.main(ActivityThread.java:7948) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1075) 
2021-05-26 06:33:56.972 24315-24315/com.example.customnavdrawerapp I/Process: Sending signal. PID: 24315 SIG: 9

请帮我解决问题。我最近开始 android 开发,如果这个问题没有通过网站的标准,我很抱歉。

没有看到 XML 你似乎在使用两个不同的库。 import android.widget.Toolbar; //you are using this in you code but using this androidx.appcompat.widget.Toolbar in your XML

它们不一样,所以当你这样做的时候

toolbarr = findViewById(R.id.toolbar);

toolbarr = android.widget.Toolbar

findViewById(R.id.toolbar); = androidx.appcompat.widget.Toolbar

简单修复删除 import android.widget.Toolbar;

并添加

import androidx.appcompat.widget.Toolbar