应用程序不断停止,logcat 错误无法理解
Posted
技术标签:
【中文标题】应用程序不断停止,logcat 错误无法理解【英文标题】:App keeps stopping, logcat error not understandable 【发布时间】:2021-08-15 21:26:15 【问题描述】:请原谅我糟糕的问题起草技巧,请帮助我理解 logcat 中显示的错误,我认为这是我的应用停止的原因
我的主活动文件
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()
指向第 34 行的 Logcat 错误 setUpToolBar() 和第 93 行 setSupportActionBar(toolbar) 在主要活动中 我的 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 ComponentInfocom.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 开发人员,也是堆栈溢出的新手。任何建议都是公开欢迎的。提前致谢。
【问题讨论】:
当这个活动已经有一个操作栏/工具栏时它说的错误 也分享你的themes.xml代码。 将您的主题父级更改为“Theme.MaterialComponents.Light.NoActionBar”或删除活动 xml 中的工具栏 【参考方案1】:错误提示
这个 Activity 已经有一个由窗口装饰提供的操作栏。 不要请求 Window.FEATURE_SUPPORT_ACTION_BAR 并设置 在您的主题中将 windowActionBar 设置为 false 以使用工具栏。
您需要更改themes.xml
中的代码。
它应该有这样的线条。
<style name="Theme.projectName" parent="Theme.MaterialComponents.Light.NoActionBar">
NoActionBar
很重要。它将禁用默认的操作栏。
【讨论】:
非常感谢!有效!我正在使用最新版本的 android studio,它有两个主题 xml 文件,一个用于深色,一个用于浅色。我忘了在黑暗中改变它。 很高兴知道。您可以将此答案标记为已接受。以上是关于应用程序不断停止,logcat 错误无法理解的主要内容,如果未能解决你的问题,请参考以下文章