使用工具栏代替Actionbar,但无法取代成功

时间:2015-07-15 14:32:41

标签: android material-design

我尝试使用工具栏而不是Actionbar,但得到的错误如下:

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

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中,我使用这些风格

的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">

为什么我还能得到这个?

3 个答案:

答案 0 :(得分:2)

如果您看到错误,那么您的问题就是SearchActivity,并且该活动仍有操作栏。

为您需要添加的活动主题

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

答案 1 :(得分:0)

可能是AppBaseTheme

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

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

最后两个是为了向后兼容

答案 2 :(得分:0)

你把这个放在清单xml文件上。

android:theme="@android:style/Theme.NoTitleBar.Fullscreen"`

style.xml

<style name="AppTheme.Base" parent="Theme.AppCompat">
<item name="colorPrimary">@color/mycolorprimary</item>
<item name="colorPrimaryDark">@color/mycolorprimarydark</item>
<item name="android:windowNoTitle">true</item>
<item name="windowActionBar">false</item>

activity_layout.xml

<?xml version="1.0" encoding="utf-8"?>

<!-- dont use this if you only want to use the actionbar instead -->

<android.support.v7.widget.Toolbar
    android:id="@+id/toolbar"
    android:layout_height="wrap_content"
    android:layout_width="match_parent"
    android:elevation="5dp"
    android:minHeight="?attr/actionBarSize"
    android:background="?attr/colorPrimary" />

适用于您的活动类

 public class MyActivity extends ActionBarActivity {

   Toolbar mToolbar;

   public void onCreate(Bundle savedInstanceState) {

      mToolbar= (Toolbar) findViewById(R.id.toolbar);
      // configure toolbar stuff
      setSupportActionBar(mToolbar);

      // or if you don't want to use the toolbar
      // then change the style values accordingly
      // and then you can run getSupportActionBar() instead

   }

}

希望这会对你有所帮助。希望你把刻度标记给别人帮助。