应用程序一直停止,logcat 错误无法理解

App keeps stopping, logcat error not understandable

请原谅我糟糕的问题起草技巧,请帮助我理解 logcat 中显示的错误,我认为这是我的应用停止的原因

我的主Activity文件

    package com.syddhoo.bookapp.pack
    
    import androidx.appcompat.app.AppCompatActivity
    import android.os.Bundle
    import android.view.MenuItem
    import android.widget.FrameLayout
    import androidx.appcompat.widget.Toolbar
    import androidx.appcompat.app.ActionBarDrawerToggle
    import androidx.core.view.GravityCompat
    import androidx.drawerlayout.widget.DrawerLayout
    import androidx.coordinatorlayout.widget.CoordinatorLayout
    import com.google.android.material.navigation.NavigationView
    import com.syddhoo.bookapp.*
    
    class MainActivity : AppCompatActivity() {
        lateinit var drawerLayout: DrawerLayout
        lateinit var coordinatorLayout: CoordinatorLayout
        lateinit var toolbar: Toolbar
        lateinit var frameLayout: FrameLayout
        lateinit var navigationView: NavigationView

    var previousMenuItem : MenuItem? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        drawerLayout = findViewById(R.id.drawerLayout)
        coordinatorLayout = findViewById(R.id.coordinatorLayout)
        toolbar = findViewById(R.id.toolbar)
        frameLayout = findViewById(R.id.frameLayout)
        navigationView = findViewById(R.id.navigationView)
        setUpToolBar()

        openDashboard()

        val actionBarDrawerToggle = ActionBarDrawerToggle(this@MainActivity, drawerLayout,
            R.string.open_drawer,
            R.string.close_drawer
        )
        drawerLayout.addDrawerListener(actionBarDrawerToggle)
        actionBarDrawerToggle.syncState()
        navigationView.setNavigationItemSelectedListener {
            if(previousMenuItem != null){
                previousMenuItem?.isChecked = false
            }
            it.isCheckable = true
            it.isChecked = true
            previousMenuItem = it

            when(it.itemId){
                R.id.dashboard ->{
                    openDashboard()
                    drawerLayout.closeDrawers()
                }
                R.id.favourite ->{
                    supportFragmentManager.beginTransaction().replace(
                        R.id.frameLayout,
                        FavouritesFragment()
                    )
                        .commit()

                    supportActionBar?.title ="Favourites"
                    drawerLayout.closeDrawers()
                }
                R.id.profile ->{
                    supportFragmentManager.beginTransaction().replace(
                        R.id.frameLayout,
                        ProfileFragment()
                    )
                        .commit()

                    supportActionBar?.title ="Profile"
                    drawerLayout.closeDrawers()
                }
                R.id.about ->{
                    supportFragmentManager.beginTransaction().replace(
                        R.id.frameLayout,
                        AboutAppFragment()
                    )
                        .commit()
                    
                    supportActionBar?.title ="About App"
                    drawerLayout.closeDrawers()
                }
            }
            return@setNavigationItemSelectedListener true
        }
    }

    fun setUpToolBar(){
        setSupportActionBar(toolbar)
        supportActionBar?.title = "Toolbar Title"
        supportActionBar?.setHomeButtonEnabled(true)
        supportActionBar?.setDisplayHomeAsUpEnabled(true)

    }

    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        val id = item.itemId
        if(id== R.id.home){
            drawerLayout.openDrawer(GravityCompat.START)

        }
        return super.onOptionsItemSelected(item)
    }
    fun openDashboard(){
        val fragment = DashboardFragment()
        val transaction = supportFragmentManager.beginTransaction()
        transaction.replace(R.id.frameLayout,fragment)
        transaction.commit()
        supportActionBar?.title = "Dashboard"
        navigationView.setCheckedItem(R.id.dashboard)
    }

    override fun onBackPressed() {
        val frag = supportFragmentManager.findFragmentById(R.id.frameLayout)

        when(frag){
            !is DashboardFragment -> openDashboard()
            else -> super.onBackPressed()
        }
    }

}

Logcat 指向第 34 行的错误 setUpToolBar() 和第 93 行 setSupportActionBar(toolbar) 在主要 Activity 我的Logcat

2021-05-27 20:15:40.373 30082-30082/? E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.syddhoo.bookapp, PID: 30082
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.syddhoo.bookapp/com.syddhoo.bookapp.pack.MainActivity}: 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 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3792)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3968)
    at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85)
    at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
    at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2307)
    at android.os.Handler.dispatchMessage(Handler.java:106)
    at android.os.Looper.loop(Looper.java:246)
    at android.app.ActivityThread.main(ActivityThread.java:8506)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:602)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1139)
 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:572)
    at androidx.appcompat.app.AppCompatActivity.setSupportActionBar(AppCompatActivity.java:159)
    at com.syddhoo.bookapp.pack.MainActivity.setUpToolBar(MainActivity.kt:93)
    at com.syddhoo.bookapp.pack.MainActivity.onCreate(MainActivity.kt:34)
    at android.app.Activity.performCreate(Activity.java:8198)
    at android.app.Activity.performCreate(Activity.java:8182)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1309)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3765)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3968) 
    at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85) 
    at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135) 
    at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2307) 
    at android.os.Handler.dispatchMessage(Handler.java:106) 
    at android.os.Looper.loop(Looper.java:246) 
    at android.app.ActivityThread.main(ActivityThread.java:8506) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:602) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1139) 

错误似乎与操作栏有关。我有什么东西需要进口吗?我是一个非常初学者 android 的开发人员,也是堆栈溢出的新手。欢迎任何建议。提前致谢。

错误说

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.

您需要更改 themes.xml 中的代码。

它应该有这样的一行。

<style name="Theme.projectName" parent="Theme.MaterialComponents.Light.NoActionBar">

NoActionBar 很重要。它将禁用默认操作栏。