setSupportActionBar (androidx.appcompat.widget.Toolbar) in AppCompatActivity cannot be applied

Viewed 21643

An error showing incompatible types Android widget toolbar cannot be converted in Java compiler while working on Android Studio.

    Toolbar toolbar = (Toolbar)findViewById(R.id.toolBar);
    toolbar.setTitle("GPS PRESENCE SYSTEM");
    setSupportActionBar(toolbar);

Error: incompatible types: android.widget.Toolbar cannot be converted to androidx.appcompat.widget.Toolbar

9 Answers

Try to replace this:

import android.widget.Toolbar;

With this:

import androidx.appcompat.widget.Toolbar;

By the way, if you are using androidx. Run it's migration process. The android support libraries will not be supported in the future. You can read about it here:

AndroidX Migrating to AndroidX

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_detail);

    Toolbar toolbar = (Toolbar)findViewById(R.id.toolbar);
    setSupportActionBar(toolbar); //No Problerm

    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}

we must import androidx.appcompat.widget.Toolbar; don't import android.widget.Toolbar;

This error because your Toolbar created using android.widget.Toolbar . But you are using androidX. This also a same kind of error. java.lang.ClassCastException: androidx.appcompat.widget.Toolbar cannot be cast to android.widget.Toolbar

To solve this kind of you can add these to lines in to your MainActivity.java file (to import).

import androidx.appcompat.app.AppCompatActivity;

import androidx.appcompat.widget.Toolbar;

Make sure to remove or comment out this android.widget.Toolbar . Then again check for error like this. because sometime you may have to import more.

AndroidX is the new and improved support library

 import androidx.appcompat.widget.Toolbar;

you can check more about from here here

Androidx's toolbar is designed for backwards compatibility while

android.widget.Toolbar

is the current plattform type

Change your activity toolbar import to:

import android.support.v7.widget.Toolbar;

You are using androidx and Android both at the same time. That is throwing an error. Either use androidx or use android appcompat.

You can invoke ActivityCompat#requireViewById which allows to omit the type cast and returns a @NonNull Toolbar reference:

Toolbar toolbar = ActivityCompat.requireViewById(this, R.id.toolbar);
setSupportActionBar(toolbar);

The title can be set on the @Nullable ActionBar reference returned by AppCompatActivity#getSupportActionBar:

ActionBar actionBar = getSupportActionBar();
if (actionBar != null) {
    actionBar.setTitle("GPS PRESENCE SYSTEM");
}

simply delete

import android.widget.Toolbar;

and add

import androidx.appcompat.widget.Toolbar;

As reported by everyone else

import android.widget.Toolbar

is creating the issue.

Replace it with

import androidx.appcompat.widget.Toolbar

Mostly on android's recommendation we go for the tool and don't notice the android or androidx dependability. That causes this error.

Related