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

This Activity already has an action bar supplied by the window decor,第1张

问题: 

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.
        at androidx.appcompat.app.AppCompatDelegateImpl.setSupportActionBar(AppCompatDelegateImpl.java:581)
        at androidx.appcompat.app.AppCompatActivity.setSupportActionBar(AppCompatActivity.java:183)
        at com.example.mykotlin1.AddUpdateDishActivity.setupActionBar(AddUpdateDishActivity.kt:22)
        at com.example.mykotlin1.AddUpdateDishActivity.onCreate(AddUpdateDishActivity.kt:17)
        at android.app.Activity.performCreate(Activity.java:8000)
        at android.app.Activity.performCreate(Activity.java:7984)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1309)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3422)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3601) 
 

问题描述:通过如下代码给activity设置actionbar

    private fun setupActionBar(){
        setSupportActionBar(mBinding.toolbarAddDishActivity)
        supportActionBar?.setDisplayHomeAsUpEnabled(true)
        mBinding.toolbarAddDishActivity.setNavigationOnClickListener {
            onBackPressed()
        }
    }

mainfest中activity没有设置theme,默认是有actionbar的

原因:使用代码设置actionbar之前,先设置activity的theme为无actionbar,然后再调用代码

解决办法:

设置activity为noactionbar

         
        

 自定义theme:

   

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/870758.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-13
下一篇 2022-05-13

发表评论

登录后才能评论

评论列表(0条)

保存