添加 ActionBar - 需要使用 Theme.AppCompat 主题

Adding ActionBar - need to use a Theme.AppCompat theme

我需要添加 ActionBar.I 从我的搜索中找到的步骤。

1.Added suppoprt 库(appcompat v7 支持库)

2.Extended 到 ActionBarActivity

3.Used styles.xml 和清单

中的 AppCompat 主题

但是 运行 该项目使设备崩溃并出现错误“您需要使用 Theme.AppCompat 主题

styles.xml

<resources>

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

</style>


<style name="RatingBar" parent="@android:style/Widget.RatingBar">
    <item name="android:progressDrawable">@drawable/ratingbar</item>
    <item name="android:minHeight">48dip</item>
    <item name="android:maxHeight">48dip</item>
</style>

</resources>

清单

<application
    android:icon="@drawable/poweritsolar"
    android:label="@string/app_name" 
    >
    <activity
        android:label="@string/app_name"
        android:name=".Powerit_solarActivity" 
        android:theme="@style/AppBaseTheme">
        <intent-filter >
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity android:name=".login"></activity>
    <activity android:name=".signup"></activity>
    <activity android:name=".catagory_btns"></activity>
</application>

activity

public class catagory_btns extends ActionBarActivity{

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.catagory_btns);
    Button cat1=(Button)findViewById(R.id.cat1);


    cat1.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            Intent in=new Intent(catagory_btns.this,login.class);
            startActivity(in);
        }
    });

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // TODO Auto-generated method stub
    getMenuInflater().inflate(R.menu.main, menu);
    ActionBar actionBar = getActionBar();
    actionBar.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#00ffff")));
    return super.onCreateOptionsMenu(menu);
}

}

LogCat

01-29 05:02:21.211: E/AndroidRuntime(2305): FATAL EXCEPTION: main
01-29 05:02:21.211: E/AndroidRuntime(2305): Process: com.power.it.solar, PID: 2305
01-29 05:02:21.211: E/AndroidRuntime(2305): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.power.it.solar/com.power.it.solar.catagory_btns}: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
01-29 05:02:21.211: E/AndroidRuntime(2305):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2176)
01-29 05:02:21.211: E/AndroidRuntime(2305):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2226)
01-29 05:02:21.211: E/AndroidRuntime(2305):     at android.app.ActivityThread.access0(ActivityThread.java:135)
01-29 05:02:21.211: E/AndroidRuntime(2305):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1397)
01-29 05:02:21.211: E/AndroidRuntime(2305):     at android.os.Handler.dispatchMessage(Handler.java:102)
01-29 05:02:21.211: E/AndroidRuntime(2305):     at android.os.Looper.loop(Looper.java:137)
01-29 05:02:21.211: E/AndroidRuntime(2305):     at android.app.ActivityThread.main(ActivityThread.java:4998)
01-29 05:02:21.211: E/AndroidRuntime(2305):     at java.lang.reflect.Method.invokeNative(Native Method)
01-29 05:02:21.211: E/AndroidRuntime(2305):     at java.lang.reflect.Method.invoke(Method.java:515)
01-29 05:02:21.211: E/AndroidRuntime(2305):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
01-29 05:02:21.211: E/AndroidRuntime(2305):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
01-29 05:02:21.211: E/AndroidRuntime(2305):     at dalvik.system.NativeStart.main(Native Method)
01-29 05:02:21.211: E/AndroidRuntime(2305): Caused by: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
01-29 05:02:21.211: E/AndroidRuntime(2305):     at android.support.v7.app.ActionBarActivityDelegate.onCreate(ActionBarActivityDelegate.java:111)
01-29 05:02:21.211: E/AndroidRuntime(2305):     at android.support.v7.app.ActionBarActivityDelegateICS.onCreate(ActionBarActivityDelegateICS.java:58)
01-29 05:02:21.211: E/AndroidRuntime(2305):     at android.support.v7.app.ActionBarActivity.onCreate(ActionBarActivity.java:98)
01-29 05:02:21.211: E/AndroidRuntime(2305):     at com.power.it.solar.catagory_btns.onCreate(catagory_btns.java:20)
01-29 05:02:21.211: E/AndroidRuntime(2305):     at android.app.Activity.performCreate(Activity.java:5243)
01-29 05:02:21.211: E/AndroidRuntime(2305):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
01-29 05:02:21.211: E/AndroidRuntime(2305):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2140)

将 Java 继承从 ActionBarActivity 更改为 Activity,并保持 manifest 中的主题不变。

更多你可以查看这个http://developer.android.com/guide/topics/ui/actionbar.html

希望对您有所帮助:)!

替换

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

<style name="AppBaseTheme" parent="android:Theme.AppCompat.Light">

检查你的值文件夹中没有几个 styles.xml,如果你支持很多 OS 版本,很可能是 IDE 生成了那些给你。