Search code examples
javaandroidandroid-studioandroid-widgetandroid-toolbar

Why mToolbar and Toolbar doesn't work in android studio


I tried everything. But I can't It figure out. Always tells me that they doesn't work--->mToolbar and Toolbar<---

import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.widget.Toast;
import android.widget.Toolbar;      

import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;
import androidx.drawerlayout.widget.DrawerLayout;
import androidx.recyclerview.widget.RecyclerView;

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

public class MainActivity extends AppCompatActivity {

    private NavigationView navigationView;
    private DrawerLayout drawerLayout;
    private RecyclerView postList;
    private Toolbar mToolbar;
    private Toolbar supportActionBar;



mToolbar = (Toolbar) findViewById(R.id.main_page_toolbar);
setSupportActionBar(mToolbar);
getSupportActionBar().setTitle("home");

Also my Grandle. I don't know if it helps.

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'androidx.appcompat:appcompat:1.0.2'
    implementation 'com.google.android.material:material:1.0.0'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    implementation 'com.google.firebase:firebase-auth:16.0.5'
    implementation 'com.google.firebase:firebase-database:16.0.4'
    implementation 'com.google.firebase:firebase-storage:16.0.4'
    implementation 'de.hdodenhof:circleimageview:3.0.2'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test.ext:junit:1.1.0'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1'
}

I tried android.widget.support.v7.Toolbar; but It didn't help

The mistake was :

from --> import android.widget.Toolbar

To --> import androidx.appcompat.widget.Toolbar


Solution

  • you are using androidX in your xml file, but in your java file you import the old library , try to remove

       import android.widget.Toolbar
    

    and put the suitable one that you imported before in xml

      import androidx.appcompat.widget.Toolbar