简体   繁体   中英

androidx.appcompat.widget.toolbar cannot be cast to android.widget.toolbar

my tollbar display is not showing in app and it is giving this error androidx.appcompat.widget.toolbar cannot be cast to android.widget.toolbar caused by Caused by: java.lang.ClassCastException

My xml toolbar code is

<androidx.appcompat.widget.Toolbar
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/allNotifications_tollbar"
        android:layout_alignParentTop="true"
        android:background="@color/LighterGrayColor"
        android:minHeight="?attr/actionBarSize"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
        />



 import android.widget.Toolbar;
    
     private void attachJavaToXML()
        {
            try
            {
                objectDialog =new Dialog(this);
                objectDialog.setContentView(R.layout.please_wait_dialog);
    
                objectToolbar=findViewById(R.id.allNotifications_tollbar);
                objectRecyclerView=findViewById(R.id.allNotifications_RecyclerView);
    
                objectToolbar.inflateMenu(R.menu.all_notifications_menu);
                objectToolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
                    @Override
                    public boolean onMenuItemClick(MenuItem item) {
                        switch (item.getItemId())
                        {
                            case R.id.allNotifications_item_clear:
                                clearAllNotifications();
                                return true;
                            case R.id.allNotifications_item_goBack:
                                startActivity(new Intent(AllNotifications.this,MainContentPage.class));
                                return true;
                        }
                        return false;
                    }
                });
            }
            catch (Exception e)
            {
                Toast.makeText(this, e.getMessage(), Toast.LENGTH_SHORT).show();
            }
        }
    }

i know there is an issue with import android.widget.Toolbar; so which import should i use??

Since in your layout you are using

<androidx.appcompat.widget.Toolbar
  android:id="@+id/allNotifications_tollbar"
  ../>

in your code you have to import the same class :

import androidx.appcompat.widget.Toolbar

Toolbar objectToolbar;
objectToolbar=findViewById(R.id.allNotifications_tollbar);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM