I made Tab Bar using Fragment on Android, but there was an error, so I'm asking you this question.

Asked 1 years ago, Updated 1 years ago, 102 views

public class MainActivity extends AppCompatActivity {
    Toolbar toolbar;
    Fragment fragment1;
    Fragment fragment2;
    Fragment fragment3;

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

        toolbar = (Toolbar)findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        ActionBar actionBar = getSupportActionBar();
        actionBar.setDisplayShowTitleEnabled(false);

        //...
    }
}

MainActivity in format.I coded inside java. When I build, 'setSupportActionBar()' error occurs, but I don't know why I get this error.

This is how the log of the cause of the error appears.

Caused by: java.lang.IllegalStateException: This Activity already has an action bar supplied by the window decor. Do not request Window.FEATURE_SUPPORT_ACTION_BAR and set windowActionBar to false in your theme to use a Toolbar instead.

android fragment tab menu toolbar

2022-09-22 21:10

1 Answers

Error when using the toolbar with the default action bar specified. Therefore, to use the toolbar as an action bar, you must disable the default action bar. The simplest way is to have the activity theme inherit Theme.AppCompat.NoActionBar from the styles.xml file.

Alternatively, add the following to the activity theme property:

<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>


2022-09-22 21:10

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.