javaandroidnavigation-drawerandroid-toolbarandroid-togglebutton

What is the error in my custom drawer navigation toolbar?


My application crashes as soon as I try to link my custom toolbar with ActionBarDrawerToggle. Following is my code:

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;
            }
        });

    }
}

I have added comments in the code to show what is causing the error.

The application keeps stopping if I define the toolbar.

stack trace:

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

Kindly help me resolve the issue. I have recently started android development so sorry if this question does not pass the site's standards.


Solution

  • Without see the XML you seem to be using two different libraries. import android.widget.Toolbar; //you are using this in you code but using this androidx.appcompat.widget.Toolbar in your XML

    They are not the same so when you are doing this

    toolbarr = findViewById(R.id.toolbar);

    toolbarr = android.widget.Toolbar

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

    Simple fix remove import android.widget.Toolbar;

    and add

    import androidx.appcompat.widget.Toolbar