使用工具栏代替操作栏,但不能代替成功 [英] use Toolbar instead Actionbar, but can not replace success

查看:55
本文介绍了使用工具栏代替操作栏,但不能代替成功的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用工具栏而不是操作栏,但出现如下错误:

I try to use Toolbar instead of Actionbar,but get the error like:

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.xjtu.wangshuai.one/com.xjtu.wangshuai.onemore.searchandscanqrcode.SearchActivity}: 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:2079)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2104)
            at android.app.ActivityThread.access$600(ActivityThread.java:132)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1157)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:4575)
            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:789)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:556)
            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.xjtu.wangshuai.onemore.searchandscanqrcode.SearchActivity.onCreate(SearchActivity.java:22)
            at android.app.Activity.performCreate(Activity.java:4465)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2033)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2104)
            at android.app.ActivityThread.access$600(ActivityThread.java:132)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1157)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:4575)
            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:789)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:556)
            at dalvik.system.NativeStart.main(Native Method)

我已经设置了AppTheme NoActionBar

I have set the AppTheme NoActionBar

styles.xml

styles.xml

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

    <style name="AppTheme" parent="AppBaseTheme">
        <item name="windowActionBar">false</item>
    </style>

    <style name="SearchAcvitityTheme" parent="Theme.AppCompat.Light">
        <item name="colorPrimary">@color/orange_yellow</item>
        <item name="colorPrimaryDark">@color/orange_yellow</item>
        <item name="colorAccent">@color/orange_yellow</item>
    </style>
</resources>

在AndroidManifest中,我使用这些样式

In the AndroidManifest,I use these style

AndroidManifest.xml

AndroidManifest.xml

<application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name"
            android:screenOrientation="portrait" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>          
        <activity
            android:name=".searchandscanqrcode.SearchActivity"
            android:theme="@style/SearchAcvitityTheme"
            android:screenOrientation="portrait"
            android:label="@string/title_activity_search" >
        </activity>
    </application>

但是我仍然得到错误. 我已经使用了 <item name="windowActionBar">false</item><style name="AppBaseTheme" parent="Theme.AppCompat.Light.NoActionBar">

but I still get the error. I have use the <item name="windowActionBar">false</item> and <style name="AppBaseTheme" parent="Theme.AppCompat.Light.NoActionBar">

我为何仍能得到这个的任何想法?

any ideas why I still get this?

推荐答案

如果您读取错误,则说明问题出在您的SearchActivity上,并且该活动仍然具有操作栏.

If you read the error your problem is your SearchActivity and that activity still has an actionbar.

对于该活动的主题,您需要添加

for your theme for that activity you need to add

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

这篇关于使用工具栏代替操作栏,但不能代替成功的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆