【问题标题】:Android Toolbar Crash Issue - Not all devicesAndroid 工具栏崩溃问题 - 并非所有设备
【发布时间】:2021-05-14 05:48:13
【问题描述】:

我遇到了一个奇怪的问题。我有一个应用程序在一堆设备上运行,但在一个设备上它失败了,看起来很简单,我无法弄清楚。

工具栏在我的所有设备上都可以正常工作 - 但会导致此错误:

不要请求 Window.FEATURE_SUPPORT_ACTION_BAR 并在您的主题中将 windowActionBar 设置为 false 以改用工具栏。

activity_main.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);

Logcat 错误:

2021-02-14 14:04:37.574 13026-13026/com.edtest.devicetools E/AndroidRuntime: 致命异常: main 进程: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) 在 android.app.ActivityThread$H.handleMessage(ActivityThread.java: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) 原因:java.lang.IllegalStateException:此活动已经有一个由窗口装饰提供的操作栏。不要在主题中请求 Window.FEATURE_SUPPORT_ACTION_BAR 并将 windowActionBar 设置为 false 以使用工具栏。 在 androidx.appcompat.app.AppCompatDelegateImpl.setSupportActionBar(AppCompatDelegateImpl.java:572) 在 androidx.appcompat.app.AppCompatActivity.setSupportActionBar(AppCompatActivity.java:159) 在 com.edtest.devicetools.MainActivity.onCreate(MainActivity.java:54) 在 android.app.Activity.performCreate(Activity.java:8183) 在 android.app.Activity.performCreate(Activity.java:8167) 在 android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1309) 在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3735) 在 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) 在 android.app.ActivityThread$H.handleMessage(ActivityThread.java: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

应用程序在某些设备上每次都会在 setSupportActionBar 上崩溃,但在其他设备上则不会。所有设备都运行 Android 10,因此没有操作系统差异。

我已阅读有关工具栏的所有文章 我尝试了带有和不带有操作栏的父主题的各种组合 我可以离开或删除主题 windowactionbar 和 windownotitle 语句,无论如何似乎都有相同的结果。

有什么想法吗?

谢谢!

【问题讨论】:

  • 您导入了哪个工具栏?确保导入 androidx 工件。
  • 在主要活动中我有 import androidx.appcompat.widget.Toolbar;还有一个我应该在里面吗?
  • 不,那是正确的。如果可能,分享 logcat。
  • 感谢 cmets - 我在上面添加了 logcat

标签: java android toolbar


【解决方案1】:

好的 - 我知道发生了什么。太令人沮丧了,我错过了这个 - 弄清楚其中的一些东西真是太疯狂了。

由于某种原因,主题让我感到困惑 - 但这是我以后去弄清楚的。

这是出了什么问题:

新的 Android Studio 项目有 theme.xml 并且还包括另一个主题.xml(夜间)

我已经将我所有的应用栏样式添加到了themes.xml 中——但我没有对这个文件的(夜间)版本做任何事情。

我遇到错误的设备在 Android 设置中设置为深色模式。

所以应用程序试图使用备用的主题.xml 并且失败了。

因此,如果您还没有准备好在您的应用中使用双重人格,请关闭该文件 或者拥抱它并支持暗模式

无论如何 - 这是我的问题。

【讨论】:

    【解决方案2】:

    正如我在您提供的主题中看到的那样

       <item name="windowNoTitle">true</item>
    

    改变它

       <item name="windowNoTitle">false</item>
    

    也许它有帮助!

    【讨论】:

    • 是的 - 我试过了 - 挑战不是这个或标题栏如何进入应用程序的某种组合 - 它最终成为夜间/黑暗模式的第二个主题的事实而且我没有将所有正确的部分添加到该文件中。
    【解决方案3】:

    我在尝试在物理设备上运行应用程序时遇到了类似的问题。

    在模拟器上效果很好,因为在模拟器上选择了浅色主题,但在物理设备上,它是深色主题。

    在物理设备上将其更改为浅色主题可使应用程序正常运行。

    我的 theme.xml 是Theme.MaterialComponents.Light.NoActionBar

    我在Activity中设置工具栏为setSupportActionBar(toolbar)

    【讨论】:

    • 是的 - 同样的事情 - 我没有意识到 - 或者没有给予足够的关注 - 最新的 Android Studio 会自动将夜间主题添加到一个新的空白项目中。并且 logcat 并没有真正指出任何关于夜间/暗模式与亮模式的内容
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-27
    • 1970-01-01
    • 2020-11-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多