【问题标题】:I am making one Android App and when I build it and run, the app stops and shows following error so what can I do?我正在制作一个 Android 应用程序,当我构建并运行它时,应用程序停止并显示以下错误,我该怎么办?
【发布时间】:2020-08-25 16:06:06
【问题描述】:

当我构建和运行我的代码时,我得到了 RunTimeExpection。这里是堆栈跟踪:

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.besecure/com.example.besecure.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:2817)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2895)
    at android.app.ActivityThread.-wrap11(Unknown Source:0)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1616)
    at android.os.Handler.dispatchMessage(Handler.java:106)
    at android.os.Looper.loop(Looper.java:176)
    at android.app.ActivityThread.main(ActivityThread.java:6651)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:547)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:824)
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:421)
    at androidx.appcompat.app.AppCompatActivity.setSupportActionBar(AppCompatActivity.java:150)
    at com.example.besecure.MainActivity.onCreate(MainActivity.java:29)
    at android.app.Activity.performCreate(Activity.java:7088)
    at android.app.Activity.performCreate(Activity.java:7079)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1215)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2770)
    ... 9 more

请帮帮我。谢谢

【问题讨论】:

标签: android exception runtimeexception


【解决方案1】:

根据Nadir Belhaj的回答和MMG cmets,

styles.xml试试这个代码:

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

【讨论】:

    【解决方案2】:

    您应该使用工具栏而不是操作栏。因此,在您的 styles.xml 中定义一个如下所示的主题并将其分配给清单中的应用程序或活动:

    styles.xml:

    <style name="AppTheme" parent="Theme.AppCompat.Light">
        <item name="windowActionBar">false</item>
    </style>
    

    或者:

    <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <!-- Customize your theme here. -->
    </style>
    

    清单:

    <application
       ....
        android:icon="@drawable/xxx"
        android:theme="@style/AppTheme">
    </application>
    

    <activity android:name="xxx"  android:theme="@style/AppTheme"/>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-12-16
      • 2023-03-28
      • 1970-01-01
      • 2011-08-30
      • 2011-03-25
      • 2020-07-06
      相关资源
      最近更新 更多