android开发分享该活动已经有一个由窗口装饰提供的操作栏

试图移动我的东西来使用Toolbar而不是操作栏,但我不断收到错误说

 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.tyczj.weddingalbum/com.xxx.xxx.MainActivity}: java.lang.IllegalStateException: This Activity already has an action bar supplied by the window decor. Do not request Window.FEATURE_ACTION_BAR and set windowActionBar to false in your theme to use a Toolbar instead. at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230) at android.app.ActivityThread.access$600(ActivityThread.java:141) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loop(Looper.java:137) at android.app.ActivityThread.main(ActivityThread.java:5039) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:511) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) at dalvik.system.NativeStart.main(Native Method) Caused by: java.lang.IllegalStateException: This Activity already has an action bar supplied by the window decor. Do not request Window.FEATURE_ACTION_BAR and set windowActionBar to false in your theme to use a Toolbar instead. at android.support.v7.app.ActionBarActivityDelegateBase.setSupportActionBar(ActionBarActivityDelegateBase.java:165) at android.support.v7.app.ActionBarActivity.setSupportActionBar(ActionBarActivity.java:92) at com.xxx.xxx.MainActivity.onCreate(MainActivity.java:113) at android.app.Activity.performCreate(Activity.java:5104) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)            at android.app.ActivityThread.access$600(ActivityThread.java:141)            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)            at android.os.Handler.dispatchMessage(Handler.java:99)            at android.os.Looper.loop(Looper.java:137)            at android.app.ActivityThread.main(ActivityThread.java:5039)            at java.lang.reflect.Method.invokeNative(Native Method)            at java.lang.reflect.Method.invoke(Method.java:511)            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)            at dalvik.system.NativeStart.main(Native Method) 

所以然后我添加我的风格为我的活动没有操作栏

 <style name="AppCompatTheme" parent="@style/Theme.AppCompat.Light"> <item name="android:windowActionBar">false</item> </style> 

主题适用于我清单中的活动

 <activity android:name=".MainActivity" android:windowSoftInputMode="adjustResize|stateHidden" android:theme="@style/AppCompatTheme" android:screenOrientation="portrait"/> 

MainActivity扩展了GooglePlayServiceActivity,所以我也设置了主题

 <activity android:name=".GooglePlayServicesActivity" android:label="@string/title_activity_google_play_services" android:theme="@style/AppCompatTheme"> 

但我仍然得到错误。 我也不要求任何地方的窗口function。 任何想法,为什么我仍然得到这个?

    我认为你正在开发的Android棒棒糖,但无论如何,包括这一行:

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

    到你的app/src/main/res/values/styles.xml的主题声明。

    另外,如果您使用的是版本22.1或更高版本的AppCompatActivity支持库,请添加以下行:

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

    所有这些添加之后,您的主题声明可能如下所示:

     <!-- Base application theme. --> <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar"> <!-- Customize your theme here. --> <item name="colorPrimary">@color/colorPrimary</item> <item name="colorPrimaryDark">@color/colorPrimaryDark</item> <item name="colorAccent">@color/colorAccent</item> <item name="windowActionBar">false</item> <item name="windowNoTitle">true</item> </style> 

    另一种简单的方法是让你的主题像Theme.AppCompat.Light.NoActionBar这样的孩子:

     <style name="NoActionBarTheme" parent="Theme.AppCompat.Light.NoActionBar"> ... </style> 

    AndroidManifest添加单行android:theme="@style/AppTheme.NoActionBar"以完成activity


    AndroidManifest.xml

     <activity android:name=".activity.YourActivity" android:theme="@style/AppTheme.NoActionBar"><!-- ADD THIS LINE --> 

    styles.xml

     <style name="AppTheme.NoActionBar"> <item name="windowActionBar">false</item> <item name="windowNoTitle">true</item> </style> 

    要将工具栏用作操作栏,请先禁用装饰提供的操作栏。

    最简单的方法是让你的主题延伸

    Theme.AppCompat.NoActionBar

    (或其轻型变体)。

    其次,通常通过您的布局XML创build一个工具栏实例:

     <android.support.v7.widget.Toolbar android:id=”@+id/my_awesome_toolbar” android:layout_height=”wrap_content” android:layout_width=”match_parent” android:minHeight=”?attr/actionBarSize” android:background=”?attr/colorPrimary” /> 

    然后在您的活动或片段中,将工具栏设置为您的操作栏:

     @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.blah); Toolbar toolbar = (Toolbar) findViewById(R.id.my_awesome_toolbar); setSupportActionBar(toolbar); } 

    此代码为我工作。

    如果您想将某些活动与操作栏和其他活动组合在一起,则应使用基本主题启用操作栏,然后创build一个子主题,以便在不需要操作栏的活动上使用它

    例如,你可以使用这样的子风格

      <style name="AppTheme.NoActionBar"> <item name="windowActionBar">false</item> <item name="windowNoTitle">true</item> </style> 

    基本主题延伸说

      <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar"> 

    然后在activity标签中使用AndroidManifest文件中的非操作栏主题

      <activity android:name="com.example.NonActionBarActivity" android:theme="@style/AppTheme.NoActionBar" 

    您必须将其应用到不需要操作栏的每个单独活动,因此如果您的项目需要的操作栏活动less于非活动栏活动,则最好将其应用于基本主题级别

    把这个添加到你的values / styles.xml中

     <style name="YourCustomTheme" parent="Theme.AppCompat.Light.NoActionBar"> </style> <style name="AppBaseTheme" parent="YourCustomTheme"> </style> <style name="AppTheme" parent="AppBaseTheme"> </style> 

    并将下列代码添加到values-v11 / styles.xml和values-v14 / styles.xml中

     <style name="AppBaseTheme" parent="YourCustomTheme"> </style> 

    而已。 它会工作。

    转到您的项目的“style.xml”,并将windowActionBar设置为false

     <style name="AppCompatTheme" parent="@style/Theme.AppCompat.Light"> <item name="android:windowActionBar">false</item> </style> 

    将这两行添加到位于style.xml中的应用程序主题中: –

      <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar"> <!-- Customize your theme here. --> <item name="colorPrimary">@color/colorPrimary</item> <item name="colorPrimaryDark">@color/colorPrimaryDark</item> <item name="colorAccent">@color/colorAccent</item> //Add this two line will fix your problem <item name="windowNoTitle">true</item> <--1(this applies after 22.0.1 support library i think <item name="windowActionBar">true</item> <--2 

    你需要改变

      <activity android:name=".YOUR ACTIVITY" android:theme="@style/AppTheme.NoActionBar" /> </application>` 

    在清单中的这些行,它将完全为我工作。

    我也面临同样的问题。 但是我用了:

    getSupportActionBar().hide();

    之前

    setContentView(R.layout.activity_main);

    现在它正在工作。

    我们可以在Style.xml中尝试其他选项,

     <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar"> <item name="colorPrimary">@color/colorPrimary</item> <item name="colorPrimaryDark">@color/colorPrimaryDark</item> <item name="colorAccent">@color/colorAccent</item> </style> 

    我有工具栏添加在我的XML。 然后在我的活动中,我添加了这个声明:

     setSupportActionBar(toolbar); 

    删除这个为我工作。 我希望它可以帮助别人。

    如果您正在使用Appcompact活动,请在您的主题中使用这三行。

     <item name="windowNoTitle">true</item> <item name="windowActionBar">false</item> <item name="android:windowActionBarOverlay">false</item> 

    简而言之,你可以做到以下几点:

     if (android.os.Build.VERSION.SDK_INT >= 21) { Window window = getWindow(); window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS); window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS); window.setStatusBarColor(getResources().getColor(R.color.colorPrimaryDark)); } 

    这就是我解决问题的方法。 在你的AndroidMainfest.xml添加下面的代码

     <activity android:name=".YourClass" android:theme="@style/Theme.AppCompat.Light.NoActionBar"> </activity> 

    另外一个较短版本的风格可以帮助:

     <style name="YourActivityTheme" parent="Theme.AppCompat.NoActionBar"> ... </style> 

    我通过删除这一行来解决它:

    android:theme="@style/Theme.MyCompatTheme"

    Manifest file中的活动属性

      以上就是android开发分享该活动已经有一个由窗口装饰提供的操作栏相关内容,想了解更多android开发(异常处理)及android游戏开发关注计算机技术网(www.ctvol.com)!)。

      本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

      ctvol管理联系方式QQ:251552304

      本文章地址:https://www.ctvol.com/addevelopment/517006.html

      (0)
      上一篇 2020年12月4日
      下一篇 2020年12月4日

      精彩推荐