use Toolbar instead Actionbar, but can not replace success

て烟熏妆下的殇ゞ 提交于 2019-12-25 16:38:35

问题


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)

I have set the 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>

In the AndroidManifest,I use these style

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>

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?


回答1:


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>



回答2:


maybe for AppBaseTheme

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

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

last two are for backward compatibility




回答3:


you put this one on manifest xml file.

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

For your Activity Class like

 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

   }

}

Hope this it will help you. Hope you put tick mark it will help to others.



来源:https://stackoverflow.com/questions/31433321/use-toolbar-instead-actionbar-but-can-not-replace-success

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!