java.lang.IllegalStateException:Activity already has an action bar supplied by the window decor.Donot request Window.FEATURE_SUPPORT_ACTION_BAR

huangapple go评论78阅读模式
英文:

java.lang.IllegalStateException:Activity already has an action bar supplied by the window decor.Donot request Window.FEATURE_SUPPORT_ACTION_BAR

问题

我正在向应用程序的仪表板添加侧边导航,在此过程中我在仪表板的布局文件中添加了自定义工具栏。

XML文件中的工具栏代码如下:

<androidx.appcompat.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="#00FFFFFF"
            android:minHeight="?attr/actionBarSize"
            android:theme="?attr/actionBarTheme"
            app:navigationIcon="@drawable/menu_toolbar"/>

Java代码部分如下:

/*---------------HOOKS--------------*/
        drawerLayout = findViewById(R.id.drawer_layout);
        navigationView = findViewById(R.id.nav_view2);
        toolbar = findViewById(R.id.toolbar);

        /*------------------Tool Bar-----------------------*/
        setSupportActionBar(toolbar);
        /*---------------Navigation Drawer Menu--------------*/
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, drawerLayout, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        drawerLayout.addDrawerListener(toggle);
        toggle.syncState();

在运行时,应用程序崩溃,并在日志中显示以下错误消息:

2020-10-17 16:49:52.568 24803-24803/com.example.bottomnav E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.bottomnav, PID: 24803
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.bottomnav/com.example.bottomnav.ClientBottomNav}: 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.example.bottomnav.ClientBottomNav.onCreate(ClientBottomNav.java:60)
        at android.app.Activity.performCreate(Activity.java:7893)
        at android.app.Activity.performCreate(Activity.java:7880)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1307)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3286)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3460)
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)
        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:2047)
        at android.os.Handler.dispatchMessage(Handler.java:107)
        at android.os.Looper.loop(Looper.java:224)
        at android.app.ActivityThread.main(ActivityThread.java:7592)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:539)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:950)
     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.example.bottomnav.ClientBottomNav.onCreate(ClientBottomNav.java:60)
        at android.app.Activity.performCreate(Activity.java:7893)
        at android.app.Activity.performCreate(Activity.java:7880)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1307)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3286)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3460)
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)
        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:2047)
        at android.os.Handler.dispatchMessage(Handler.java:107)
        at android.os.Looper.loop(Looper.java:224)
        at android.app.ActivityThread.main(ActivityThread.java:7592)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:539)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:950)

我认为默认情况下存在ActionBar,并且它与新工具栏存在一些问题,应该如何解决这个问题?

英文:

I am adding a Side Navigation to the dashboard of my app and while doing that I added a Custom Toolbar in the Layout File of the dashboard.

The Toolbar Code of the xml file is -

<androidx.appcompat.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="#00FFFFFF"
            android:minHeight="?attr/actionBarSize"
            android:theme="?attr/actionBarTheme"
            app:navigationIcon="@drawable/menu_toolbar"/>

The Java Code -

/*---------------HOOKS--------------*/
        drawerLayout = findViewById(R.id.drawer_layout);
        navigationView = findViewById(R.id.nav_view2);
        toolbar = findViewById(R.id.toolbar);

        /*------------------Tool Bar-----------------------*/
        setSupportActionBar(toolbar);
        /*---------------Navigation Drawer Menu--------------*/
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, drawerLayout, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        drawerLayout.addDrawerListener(toggle);
        toggle.syncState();

while running, the app crashes and the following error message is shown in the logcat -

2020-10-17 16:49:52.568 24803-24803/com.example.bottomnav E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.bottomnav, PID: 24803
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.bottomnav/com.example.bottomnav.ClientBottomNav}: 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:3311)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3460)
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)
        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:2047)
        at android.os.Handler.dispatchMessage(Handler.java:107)
        at android.os.Looper.loop(Looper.java:224)
        at android.app.ActivityThread.main(ActivityThread.java:7592)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:539)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:950)
     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.example.bottomnav.ClientBottomNav.onCreate(ClientBottomNav.java:60)
        at android.app.Activity.performCreate(Activity.java:7893)
        at android.app.Activity.performCreate(Activity.java:7880)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1307)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3286)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3460) 
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83) 
        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:2047) 
        at android.os.Handler.dispatchMessage(Handler.java:107) 
        at android.os.Looper.loop(Looper.java:224) 
        at android.app.ActivityThread.main(ActivityThread.java:7592) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:539) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:950) 

I think there is an ActionBar by default and it is having some issues with the new toolbar, what can be done to resolve this issue?

答案1

得分: 0

如错误描述本身所述:

> 引发原因:java.lang.IllegalStateException:此 Activity 已由窗口装饰提供操作栏。请勿请求 Window.FEATURE_SUPPORT_ACTION_BAR,并在主题中将 windowActionBar 设置为 false,以使用 Toolbar。

因此,您的活动已经拥有 actionBar,而您正在提供另一个。要解决此问题,请使您的主题扩展一个没有 actionBar 的父主题,或者像下面这样将您的 actionBar 设置为 false:

<style name="AppTheme" parent="您的父主题在这里">
    <!-- 在此处自定义您的主题。 -->
    .....
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/gray_dark_bg</item>
    <item name="colorAccent">@color/colorAccent</item>
    <item name="windowActionBar">false</item> <!-- 这是必需的选项 -->
    .....
</style>
英文:

as the error describes itself

> 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.

so your activity already have actionBar and you are providing one , to solve this make your theme extend parent theme which doesn't have an actionBar or set your actionBar to false like below

&lt;style name=&quot;AppTheme&quot; parent=&quot;your parent theme here&quot; //&gt;
    &lt;!-- Customize your theme here. --&gt;
    .....
    &lt;item name=&quot;colorPrimary&quot;&gt;@color/colorPrimary&lt;/item&gt;
    &lt;item name=&quot;colorPrimaryDark&quot;&gt;@color/gray_dark_bg&lt;/item&gt;
    &lt;item name=&quot;colorAccent&quot;&gt;@color/colorAccent&lt;/item&gt;
    &lt;item name=&quot;windowActionBar&quot;&gt;false&lt;/item&gt; // this is the required option
    .....

&lt;/style&gt;

huangapple
  • 本文由 发表于 2020年10月17日 20:01:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/64402233.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定