java - Android 工具栏崩溃问题 - 并非所有设备
问题描述
我遇到了一个奇怪的问题。我有一个在一堆设备上运行的应用程序,但在一个设备上它失败了,看起来很简单,我无法弄清楚。
工具栏在我的所有设备上都可以正常工作 - 但会导致此错误:
不要在主题中请求 Window.FEATURE_SUPPORT_ACTION_BAR 并将 windowActionBar 设置为 false 以使用工具栏。
活动主.xml:
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<com.google.android.material.appbar.AppBarLayout
android:id="@+id/appBarLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="@color/header_color"
app:popupTheme="@style/ThemeOverlay.AppCompat.Dark" />
</com.google.android.material.appbar.AppBarLayout>
主题.xml:
<resources xmlns:tools="http://schemas.android.com/tools">
<!-- Base application theme. -->
<style name="Theme.TestApp" parent="Theme.MaterialComponents.DayNight.Bridge">
<!-- Primary brand color. -->
<item name="colorPrimary">@color/purple_500</item>
<item name="colorPrimaryVariant">@color/purple_700</item>
<item name="colorOnPrimary">@color/white</item>
<!-- Secondary brand color. -->
<item name="colorSecondary">@color/teal_200</item>
<item name="colorSecondaryVariant">@color/teal_700</item>
<item name="colorOnSecondary">@color/black</item>
<!-- Status bar color. -->
<item name="android:statusBarColor" tools:targetApi="l">?attr/colorPrimaryVariant</item>
<!-- Customize your theme here. -->
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
<style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" >
<item name="android:background">@color/header_color</item>
</style>
显现:
<activity
android:name=".MainActivity"
android:theme="@style/Theme.TestApp">
</activity>
主要活动
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
日志错误:
2021-02-14 14:04:37.574 13026-13026/com.edtest.devicetools E/AndroidRuntime:致命异常:主进程:com.edtest.devicetools,PID:13026 java.lang.RuntimeException:无法启动活动 ComponentInfo{ com.edtest.devicetools/com.edtest.devicetools.MainActivity}:java.lang.IllegalStateException:这个活动已经有一个由窗口装饰提供的操作栏。不要在主题中请求 Window.FEATURE_SUPPORT_ACTION_BAR 并将 windowActionBar 设置为 false 以使用工具栏。在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3762) 在 android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3938) 在 android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85) 在 android。 app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135) 在 android.app。servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2277) at android.os.Handler.dispatchMessage(Handler.java:106) at android.os.Looper .loop(Looper.java:246) at android.app.ActivityThread.main(ActivityThread.java:8425) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller .run(RuntimeInit.java:596) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1130) 原因:java.lang.IllegalStateException:此活动已经有一个由窗口装饰提供的操作栏。不要在主题中请求 Window.FEATURE_SUPPORT_ACTION_BAR 并将 windowActionBar 设置为 false 以使用工具栏。在 androidx 的 androidx.appcompat.app.AppCompatDelegateImpl.setSupportActionBar(AppCompatDelegateImpl.java:572)。2277) 在 android.os.Handler.dispatchMessage(Handler.java:106) 在 android.os.Looper.loop(Looper.java:246) 在 android.app.ActivityThread.main(ActivityThread.java:8425) 在 java. lang.reflect.Method.invoke(本机方法)在 com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:596) 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1130 ) 2021-02-14 14:04:37.600 13026-13026/com.edtest.devicetools I/Process: 发送信号。PID:13026 SIG:9 600 13026-13026/com.edtest.devicetools I/Process:发送信号。PID:13026 SIG:9 600 13026-13026/com.edtest.devicetools I/Process:发送信号。PID:13026 SIG:9
该应用程序在某些设备上每次都会在 setSupportActionBar 上崩溃,但在其他设备上则不会。所有设备都运行 Android 10,因此没有操作系统差异。
我已经阅读了所有关于工具栏的文章我已经尝试了带有和不带有操作栏的父主题的各种组合我可以离开或删除主题 windowactionbar 和 windownotitle 语句,无论如何似乎都有相同的结果。
有什么想法吗?
谢谢!
解决方案
好的 - 我知道发生了什么。太令人沮丧了,我错过了这个——弄清楚这些东西真是太疯狂了。
出于某种原因,主题让我感到困惑——但那是我以后去弄清楚的。
出了什么问题:
新的 Android Studio 项目有themes.xml 并且还包括另一个themes.xml (night)
我已经将我所有的应用栏样式添加到了themes.xml 中——但我没有对这个文件的(夜间)版本做任何事情。
我遇到错误的设备在 Android 设置中设置为暗模式。
所以该应用程序试图使用备用的themes.xml 并且失败了。
因此,如果您还没有准备好在您的应用程序中使用任何双重人格,要么关闭该文件,要么接受它并支持暗模式
无论哪种方式 - 这是我的问题。
推荐阅读
- python - 在 Windows 上的 python 3.7 中运行烧瓶应用程序的问题
- reactjs - 使用 react 和 typescript 完成某些请求后,如何将用户重定向到 url“/home”?
- java - 如何在 DynamoDb 中使用一个主键和一个属性进行查询或搜索
- pmd - 如何为 XSLT 文件编写自定义 Java 规则
- python - 对齐python图形文本变量
- linux - Shell 脚本输出不完整
- python-3.x - 在 For 循环中使用它后尝试 print(i) 时未定义名称错误“i”
- python - Python:无法使用 jsonpickle 打印同一对象两次或更多次
- postgresql - 现在所有 PostgreSQL 索引都可以索引任何元素吗?
- mysql - 在 MySQL 中将值类型存储为数据库列