在定制的Andr​​oid标题栏。获取异常,您需要使用Theme.AppCompat主题您需要、标题栏、异常、主题

2023-09-03 23:03:44 作者:時間諷刺了誰的執著

搜索了很多,但没有发现有用的。

Search a lot but nothing found useful.

我创造我的自定义标题栏一个简单的应用程序,但没有得到那么远天。我用这 https://developer.android.com/training/basics/actionbar /styling.html 。

I am creating a simple app with my customized Title bar but not getting so far from days. I am using this https://developer.android.com/training/basics/actionbar/styling.html.

的Manifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.title"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="11"
        android:targetSdkVersion="17" />

    <application
        android:theme="@style/CustomActionBarTheme"//title bar
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:name="com.example.title.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

</manifest>

我创建theme.xml喜欢的 RES /价值/的themes.xml 的。

code:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <!-- the theme applied to the application or activity -->
    <style name="CustomActionBarTheme"
           parent="@style/Theme.AppCompat.Light.DarkActionBar">
        <item name="android:actionBarStyle">@style/MyActionBar</item>

        <!-- Support library compatibility -->
        <item name="actionBarStyle">@style/MyActionBar</item>
    </style>

    <!-- ActionBar styles -->
    <style name="MyActionBar"
           parent="@style/Widget.AppCompat.Light.ActionBar.Solid.Inverse">
        <item name="android:background">#CCCCCC</item>

        <!-- Support library compatibility -->
    </style>
</resources>

获取异常:( logcat的:

Getting Exception :( logcat:

04-22 11:37:21.755: E/test(11652): Exception
04-22 11:37:21.757: E/AndroidRuntime(11652): FATAL EXCEPTION: main
04-22 11:37:21.757: E/AndroidRuntime(11652): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.title/com.example.title.MainActivity}: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
04-22 11:37:21.757: E/AndroidRuntime(11652):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2306)
04-22 11:37:21.757: E/AndroidRuntime(11652):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2358)
04-22 11:37:21.757: E/AndroidRuntime(11652):    at android.app.ActivityThread.access$600(ActivityThread.java:156)
04-22 11:37:21.757: E/AndroidRuntime(11652):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1340)
04-22 11:37:21.757: E/AndroidRuntime(11652):    at android.os.Handler.dispatchMessage(Handler.java:99)
04-22 11:37:21.757: E/AndroidRuntime(11652):    at android.os.Looper.loop(Looper.java:153)
04-22 11:37:21.757: E/AndroidRuntime(11652):    at android.app.ActivityThread.main(ActivityThread.java:5299)
04-22 11:37:21.757: E/AndroidRuntime(11652):    at java.lang.reflect.Method.invokeNative(Native Method)
04-22 11:37:21.757: E/AndroidRuntime(11652):    at java.lang.reflect.Method.invoke(Method.java:511)
04-22 11:37:21.757: E/AndroidRuntime(11652):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
04-22 11:37:21.757: E/AndroidRuntime(11652):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
04-22 11:37:21.757: E/AndroidRuntime(11652):    at dalvik.system.NativeStart.main(Native Method)
04-22 11:37:21.757: E/AndroidRuntime(11652): Caused by: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
04-22 11:37:21.757: E/AndroidRuntime(11652):    at android.support.v7.app.ActionBarActivityDelegate.onCreate(ActionBarActivityDelegate.java:111)
04-22 11:37:21.757: E/AndroidRuntime(11652):    at android.support.v7.app.ActionBarActivityDelegateICS.onCreate(ActionBarActivityDelegateICS.java:58)
04-22 11:37:21.757: E/AndroidRuntime(11652):    at android.support.v7.app.ActionBarActivity.onCreate(ActionBarActivity.java:98)
04-22 11:37:21.757: E/AndroidRuntime(11652):    at com.example.title.MainActivity.onCreate(MainActivity.java:18)
04-22 11:37:21.757: E/AndroidRuntime(11652):    at android.app.Activity.performCreate(Activity.java:5182)
04-22 11:37:21.757: E/AndroidRuntime(11652):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1081)
04-22 11:37:21.757: E/AndroidRuntime(11652):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2270)
04-22 11:37:21.757: E/AndroidRuntime(11652):    ... 11 more

指导我这个请

Guide me with this please

推荐答案

您MainActivity可能是由ActionBarActivity延伸。而且你可能引用AppCompat。所以,你需要使用 Theme.AppCompat

Your MainActivity probably extends from ActionBarActivity. And you are probably referencing AppCompat. So you need to use Theme.AppCompat.

你也有安卓的minSdkVersion =11。因此,有没有需要延长 ActionBarActivty 键,无需引用 AppCompat 键,无需 Theme.AppCompat

Also you have android:minSdkVersion="11". So there is no need to extend ActionBarActivty and no need to reference AppCompat and no need for Theme.AppCompat

http://developer.android.com/guide/topics/ui/ actionbar.html

另外,请检查

https://developer.android.com/training/basics/actionbar/ styling.html