This Activity already has an action bar supplied by the window decor

后端 未结 21 2320
-上瘾入骨i
-上瘾入骨i 2020-11-21 23:45

Trying to move over my stuff to use Toolbar instead of action bar but I keep getting an error saying

java.lang.RuntimeException: Unable to start         


        
相关标签:
21条回答
  • 2020-11-22 00:48

    To use Toolbar as an Action Bar, first disable the decor-provided Action Bar.

    The easiest way is to have your theme extend from

    Theme.AppCompat.NoActionBar

    (or its light variant).

    Second, create a Toolbar instance, usually via your layout XML:

    <android.support.v7.widget.Toolbar
        android:id=”@+id/my_awesome_toolbar”
        android:layout_height=”wrap_content”
        android:layout_width=”match_parent”
        android:minHeight=”?attr/actionBarSize”
        android:background=”?attr/colorPrimary” />
    

    Then in your Activity or Fragment, set the Toolbar to act as your Action Bar:

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.blah);
    
        Toolbar toolbar = (Toolbar) findViewById(R.id.my_awesome_toolbar);
        setSupportActionBar(toolbar);
    }
    

    This code worked for me.

    0 讨论(0)
  • 2020-11-22 00:49

    Another easy way is to make your theme a child of Theme.AppCompat.Light.NoActionBar like so:

    <style name="NoActionBarTheme" parent="Theme.AppCompat.Light.NoActionBar">
         ...
    </style>
    
    0 讨论(0)
  • 2020-11-22 00:49

    I solved it by removing this line:

    android:theme="@style/Theme.MyCompatTheme"

    from activity properties in the Manifest file

    0 讨论(0)
提交回复
热议问题