试图移动我的东西使用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">
但我仍然得到错误。我也没有在任何地方请求窗口功能。任何想法为什么我仍然得到这个?
我认为你正在为Android Lollipop开发,但无论如何包括这一行:
<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>
在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
我的xml中添加了工具栏。然后在我的活动中我添加了这个声明:
setSupportActionBar(toolbar);
删除这对我有用。我希望它对某人有帮助。
如果您在此行中收到错误:
setSupportActionBar(...);
您需要检查您的活动是否指的是包含工具栏的主题。您的应用程序的AppTheme可能已包含工具栏,例如
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
而你正在尝试添加第二个。如果要使用应用程序的AppTheme,则需要在manifest.xml文件中删除活动中的主题。
例如:
<activity
android:name=".ui.activities.SettingsActivity"
android:theme="@style/AppTheme1" /> --> remove this theme
如果您正在使用Appcompact
Activity,请在主题中使用这三行。
<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>
在清单中或Activity标记内的应用程序标记内使用此选项。
机器人:主题= “@风格/ AppTheme.NoActionBar”
有两种简单快速的解决方案
转到res - > values - > styles
YOU can also watch this video for more easiness CLICK HERE
解决方案1:
您可以通过替换删除操作栏
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
至
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
溶液2:
只需添加这些行
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
所以现在你的样式文件看起来像这样
`
<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>
`
我通过删除这一行来解决它:
android:theme="@style/Theme.MyCompatTheme"
来自Manifest file
的活动属性
只需将parent="Theme.AppCompat.NoActionBar"
放在style.xml文件中即可
喜欢 - <style parent="Theme.AppCompat.NoActionBar" name="AppTheme.NoActionBar">
另一个简单的方法是让你的主题像Theme.AppCompat.Light.NoActionBar
的孩子一样:
<style name="NoActionBarTheme" parent="Theme.AppCompat.Light.NoActionBar">
...
</style>
在android:theme="@style/AppTheme.NoActionBar"
中添加单行activity
到AndroidManifest
,你已经完成了。
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创建工具栏实例:
<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);
}
这段代码对我有用。
如果你想将一些活动与动作栏和其他动作组合在一起,你应该使用基本主题启用动作栏,然后创建一个子主题,你将在不需要动作栏的活动中使用它
例如,您可以使用这样的子样式
<style name="AppTheme.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
虽然基本主题延伸说
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
然后在活动标签内的AndroidManifest文件中使用非操作栏主题说
<activity
android:name="com.example.NonActionBarActivity"
android:theme="@style/AppTheme.NoActionBar"
您必须将此应用于不需要操作栏的每个单独活动,因此如果您的项目需要的操作栏活动少于非活动栏,则最好将其应用于基本主题级别
我也面临同样的问题。但我用过:
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>
在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>
而已。它会工作。
你需要改变
<activity
android:name=".YOUR ACTIVITY"
android:theme="@style/AppTheme.NoActionBar" />
</application>`
清单中的这些行。它对我来说非常有用。
转到项目的“style.xml”并将windowActionBar设置为false
<style name="AppCompatTheme" parent="@style/Theme.AppCompat.Light">
<item name="android:windowActionBar">false</item>
</style>