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:
- Error:This Activity already has an action bar supplied by the window decor.
- Actionbar expiration problem
- classes could not be found: – android.support.v7.i
- [Solved] Android Studio Warning: Failed to load AppCompat ActionBar with unknown error
- [Solved] error: resource style/AppTheme (aka com.example.myapplication:style/AppTheme
- The image set by app: srccompat in Android constraintlayout cannot be displayed
- The Android studio preview interface reports an error render problem
- [Solved] Theme Error retrieving parent for item: No resource found that matche
- Can’t find it“ android:Theme.Holo.Light.DarkActionBar ” [How to Solove]
- Warning: Each child in an array or iterator should have a unique “key” prop. Check the render method