Search code examples
javaandroidandroid-appcompatandroidx

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


I am getting an error while developing the toolbar I have searched solution on the internet and dint gets any solution working as I am new to android development.

I have tried to change the androidx.appcompat.widget.Toolbar to android.support.v7.widget.toolbar then I am getting an error as v7 cannot be resolved

main Activity

   package com.nanb.chaton;
   import androidx.appcompat.app.AppCompatActivity;
   import androidx.appcompat.widget.Toolbar;

   import android.os.Build;
   import android.os.Bundle;

   public class MainActivity extends AppCompatActivity {
     private Toolbar main_toolbar;
     @Override
     protected void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.activity_main);

       Toolbar main_toolbar = findViewById(R.id.toolbar);
       getSupportActionBar(main_toolbar);
       getSupportActionBar().setTitle("ChatOn");
     }
   }

I am getting an error in the getSupportActionBar it says that it cannot be applied with AppCompatActivity to androidx.appcompat.widget.Toolbar


Solution

  • You can use:

    getSupportActionBar();
    

    instead of getSupportActionBar(toolbar); which doesn't exist.

    Instead if you would like to set the actionbar you have to use:

    setSupportActionBar(toolbar);