报错信息显示在MainActivity.kt文件的onCreate方法中,出现了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.\n\n这个异常的原因是在使用Toolbar时,同时又在主题中设置了ActionBar,导致冲突。解决方法是在主题中将windowActionBar属性设置为false,以使用Toolbar代替ActionBar。\n\n要解决这个问题,你可以按照以下步骤进行操作:\n\n1. 打开res/values/styles.xml文件;\n2. 在AppTheme主题中添加一个新的属性并将其设置为false,如下所示:\n\nxml\n<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">\n <!-- Customize your theme here. -->\n <item name="windowActionBar">false</item>\n <item name="colorPrimary">@color/colorPrimary</item>\n <item name="colorPrimaryDark">@color/colorPrimaryDark</item>\n <item name="colorAccent">@color/colorAccent</item>\n</style>\n\n\n3. 保存文件并重新运行应用程序。\n\n这样就可以解决该异常并正常使用Toolbar了。

Android Toolbar 使用教程:解决

原文地址: https://www.cveoy.top/t/topic/qwxJ 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录