How to Solve setSupportActionBar() Method Error

In Android development, to use the ToolBar control to replace the ActionBar control, you need to use the setSupportActionBar() method in the java code, as follows:

1 Toolbar toolbar = (Toolbar) this.findViewById(R.id.toolBar);
2 this.setSupportActionBar(toolbar);

There are two common types of errors:

1. Method parameter error


This kind of error is because the wrong class is imported, put the following code

1 import android.widget.Toolbar;

Replace with the following code

1 import android.support.v7.widget.Toolbar;

 

2. Method name error

Need to inherit ActionBarActivity class or AppCompatActivity class.

Because the ActionBarActivity class is obsolete, it is recommended to inherit the AppCompatActivity class.

Note: If you inherit the AppCompatActivity class, you need to use the Theme.AppCompat.Light.NoActionBar theme, for example

<style name="AppTheme.Base" parent="Theme.AppCompat.Light.NoActionBar">
        <item name="android:windowNoTitle">true</item>
        <item name="android:windowActionBar">false</item>
</style>

Similar Posts:

Leave a Reply

Your email address will not be published. Required fields are marked *