How to use Android's AppCompat theming applica

2019-07-21 01:23发布

IDE: Android Studio 1.1.0

ENV: Debugging on usb-connected phone using Android API v10.


I'm trying to use AppCompat to support Android API v8 and up. Compiles fine but crashes when the activity is drawn, saying:

lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.

...however, in AndroidManifest.xml I have:

    <application
        android:name=".objects.PikCMS"
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/ThemeOverlay.AppCompat"
        >

Thank you & please forgive my frustration. I've been reading A LOT of related but unhelpful info. }8,(


Other Info

  • I do not want to force a specific theme.
  • I would like to avoid using different files for different API versions. However, I don't mind adding different versions of same property next to each other.
  • I had errors in styles.xml for items introduced in v11 (like "buttonBarStyle" & "showDividers"). After reading about the issue, I had thought AppCompat would "fill in the blanks" for pre11 APIs but (in my configuration) it isn't. These are currently all commented out -- until I find out how to use them with AppCompat. Similarly, I had problems with the layout files -- these problematic properties were deleted.
  • Activity class: Login.java extends :ActionBarActivity:. I'm also importing "android.support.v7.app.ActionBarActivity" but I'm not sure if I need all of it as I do not implicitly use the action bar (but I might in the future).



Code Files

build.gradle (Module: app)

apply plugin: 'com.android.application'

android {
    compileSdkVersion 21
    buildToolsVersion '21.1.2'

    defaultConfig {
        applicationId "com.kjackson324.pikcms"
        minSdkVersion 10
        targetSdkVersion 21
        versionCode 1
        versionName "1.0"
    }
}

dependencies {
    compile 'com.android.support:support-v4:21.0.3'
    compile 'com.android.support:appcompat-v7:21.0.3'
    compile fileTree(include: ['*.jar'], dir: 'libs')
}




AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.kjackson324.pikcms" >

    <application
        android:name=".objects.PikCMS"
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/ThemeOverlay.AppCompat"
        >
        <uses-permission android:name="android.permission.BLUETOOTH" />

        <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="16" />

        <activity
            android:name=".Login"
            android:configChanges="keyboardHidden|orientation"
            android:label="@string/title_activity_login"
            android:screenOrientation="portrait" >
            <intent-filter>
                <action android:name="com.kjackson324.pikcms.Login" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".Navigation"
            android:configChanges="keyboardHidden|orientation"
            android:label="@string/title_activity_navigation"
            android:screenOrientation="portrait" >
            <intent-filter>
                <action android:name="com.kjackson324.pikcms.Navigation" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
        <activity
            android:name=".PickHistory"
            android:configChanges="keyboardHidden|orientation"
            android:label="@string/title_activity_pik_log"
            android:screenOrientation="portrait" >
            <intent-filter>
                <action android:name="com.kjackson324.pikcms.PickHistory" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>

        <!--

            ...bla, bla, bla...

            (much more of the same, no new attributes used)
        -->

        <activity
            android:name=".ConfigLocFields"
            android:label="@string/title_activity_config_loc_fields"
            android:configChanges="keyboardHidden|orientation"
            android:screenOrientation="portrait" >
            <meta-data
                android:name="android.support.PARENT_ACTIVITY"
                android:value="com.kjackson324.pikcms.Navigation" />
            <intent-filter>
                <action android:name="com.kjackson324.pikcms.ConfigLocFields" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
    </application>

</manifest>




styles.xml

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

    <style name="simple_center" parent="">
        <!--item name="android:layout_gravity">center</item-->
        <!--item name="android:gravity">center</item-->
        <item name="android:dialogLayout">?android:layout_gravity</item>
    </style>

    <style name="lblLoggedIn" parent="@android:style/TextAppearance.Small">
        <item name="android:layout_width">wrap_content</item>
        <item name="android:layout_height">wrap_content</item>
        <item name="android:text">@string/sys_logged_in</item>
        <item name="android:layout_gravity">top|right</item>
        <item name="android:gravity">top|center_vertical</item>
        <item name="android:textColor">@android:color/white</item>
        <item name="android:paddingLeft">4dp</item>
        <item name="android:paddingRight">4dp</item>
    </style>

    <style name="tvLoggedIn" parent="@android:style/TextAppearance.Small">
        <item name="android:layout_width">0dp</item>
        <item name="android:layout_height">match_parent</item>
        <item name="android:inputType">none</item>
        <item name="android:gravity">top</item>
        <item name="android:layout_gravity">top|left</item>
        <item name="android:background">@android:color/transparent</item>
        <item name="android:textColor">@android:color/white</item>
        <item name="android:layout_weight">2</item>
    </style>

    <style name="layout_master">
        <item name="android:orientation">vertical</item>
        <item name="android:layout_width">fill_parent</item>
        <item name="android:layout_height">fill_parent</item>
        <!--item name="showDividers">end</item-->
        <item name="android:layout_alignParentTop">true</item>
        <item name="android:layout_centerHorizontal">true</item>
    </style>

    <style name="layout_loggedin">
        <!--item name="buttonBarStyle">?android:attr/buttonBarStyle</item-->
        <item name="android:layout_width">fill_parent</item>
        <item name="android:layout_height">wrap_content</item>
        <item name="android:orientation">horizontal</item>
        <item name="android:background">?android:attr/dividerVertical</item>
        <item name="android:gravity">bottom|center</item>
    </style>

    <style name="layout_bodyscroll">
        <item name="android:layout_width">fill_parent</item>
        <item name="android:fillViewport">true</item>
        <item name="android:layout_height">0dp</item>
        <item name="android:layout_weight">1</item>
        <item name="android:layout_marginTop">@dimen/activity_bodyscroll_margin</item>
        <item name="android:layout_marginBottom">@dimen/activity_bodyscroll_margin</item>
        <item name="android:layout_marginLeft">@dimen/activity_bodyscroll_margin</item>
        <item name="android:layout_marginRight">@dimen/activity_bodyscroll_margin</item>
    </style>

    <style name="layout_bodylinear">
        <item name="android:orientation">vertical</item>
        <item name="android:layout_width">fill_parent</item>
        <item name="android:layout_height">wrap_content</item>
    </style>

    <style name="layout_btnbar_frame">
        <item name="android:layout_width">fill_parent</item>
        <item name="android:layout_height">fill_parent</item>
        <item name="android:layout_weight">1</item>
    </style>

    <style name="layout_btnbar_linear" parent="">
        <!--item name="buttonBarStyle">?android:attr/buttonBarStyle</item-->
        <item name="android:layout_width">fill_parent</item>
        <item name="android:layout_height">wrap_content</item>
        <item name="android:orientation">horizontal</item>
        <item name="android:background">?android:attr/dividerVertical</item>
        <item name="android:gravity">bottom|center</item>
        <item name="android:layout_gravity">bottom</item>
    </style>

    <style name="layout_btnbar_btn" parent="">
        <!--item name="buttonBarStyle">?android:attr/buttonBarStyle</item-->
        <item name="android:layout_width">wrap_content</item>
        <item name="android:layout_height">wrap_content</item>
        <item name="android:layout_weight">1</item>
        <item name="android:background">@android:color/transparent</item>
        <item name="android:textColor">@android:color/primary_text_dark</item>
        <item name="android:paddingTop">@dimen/activity_vertical_margin</item>
        <item name="android:paddingBottom">@dimen/activity_vertical_margin</item>
    </style>

    <style   name="menu_btn"    parent="layout_btnbar_btn">
        <item name="android:textAppearance">@android:style/TextAppearance.Large</item>
        <item name="android:textStyle">bold</item>
        <item name="android:layout_width">fill_parent</item>
        <item name="android:layout_height">fill_parent</item>
    </style>

    <style name="hidden">
        <item name="android:layout_width">0dp</item>
        <item name="android:layout_height">0dp</item>
        <item name="android:editable">false</item>
        <item name="android:autoText">false</item>
        <item name="android:visibility">invisible</item>
        <item name="android:inputType">none</item>
    </style>

    <style name="field_edittext">
        <item name="android:layout_width">fill_parent</item>
        <item name="android:layout_height">wrap_content</item>
        <item name="android:layout_marginBottom">7dp</item>
        <item name="android:layout_marginTop">3dp</item>
        <item name="android:layout_marginLeft">2dp</item>
        <item name="android:layout_marginRight">2dp</item>
        <item name="android:layout_column">1</item>
        <item name="android:layout_weight">2</item>
        <item name="android:textColor">@android:color/primary_text_dark</item>
        <item name="android:autoText">true</item>
        <item name="android:gravity">bottom</item>
    </style>

    <style name="tvFieldLabel">
        <item name="android:layout_height">wrap_content</item>
        <item name="android:layout_width">wrap_content</item>
        <item name="android:textAppearance">?android:attr/textAppearanceMedium</item>
        <item name="android:layout_gravity">top</item>
        <item name="android:gravity">top</item>
    </style>

    <style name="field_edittext_note" parent="field_edittext">
        <item name="android:layout_height">125dp</item>
        <item name="android:gravity">top</item>
    </style>

    <style name="tvTableField">
        <item name="android:layout_width">wrap_content</item>
        <item name="android:layout_height">match_parent</item>
        <item name="android:layout_gravity">bottom|right</item>
        <item name="android:layout_weight">0</item>
        <item name="android:layout_column">0</item>
        <item name="android:textAppearance">@android:style/TextAppearance.Medium</item>
        <item name="android:textColor">@android:color/secondary_text_dark</item>
        <item name="android:gravity">center_vertical</item>
        <item name="android:inputType">none</item>
    </style>

    <style name="spinTails">
        <item name="android:layout_width">fill_parent</item>
        <item name="android:layout_height">fill_parent</item>
        <!--item name="android:background">?android:attr/dividerVertical</item-->
        <item name="android:padding">8dp</item>
    </style>


    <style name="tvFieldColPrimary" parent="tvTableField">
        <item name="android:textColor">@android:color/primary_text_dark</item>
        <item name="android:paddingRight">8dp</item>
    </style>

    <style name="tvFieldColSecondary" parent="tvFieldColPrimary">
        <item name="android:textColor">@android:color/secondary_text_dark</item>
    </style>

</resources>




./layout/activity_login.xml (my first activity)

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.kjackson324.pikcms.Login"
    android:id="@+id/layLogin" >

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true">

        <TextView
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:id="@+id/tvId"
            android:visibility="invisible"/>

        <ScrollView
            android:layout_width="fill_parent"
            android:layout_height="0dp"
            android:id="@+id/scrollView4"
            android:layout_weight="1">

            <LinearLayout
                android:orientation="vertical"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:id="@+id/linearLayout13"
                android:paddingLeft="@dimen/activity_horizontal_margin"
                android:paddingRight="@dimen/activity_horizontal_margin"
                android:paddingTop="@dimen/activity_vertical_margin"
                android:paddingBottom="@dimen/activity_vertical_margin">

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:textAppearance="?android:attr/textAppearanceLarge"
                    android:text="@string/entry_employee"
                    android:id="@+id/tvName"
                    />

                <Spinner
                    android:id="@+id/spinEmployees"
                    android:layout_width="match_parent"
                    android:layout_height="0dp"
                    android:layout_weight="1"
                    android:layout_gravity="center_horizontal"
                    android:clickable="true"
                    android:paddingTop="@dimen/activity_vertical_margin"
                    android:paddingBottom="@dimen/activity_vertical_margin"
                    android:paddingLeft="@dimen/activity_horizontal_margin"
                    android:layout_marginLeft="@dimen/activity_horizontal_margin"
                    />

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:textAppearance="?android:attr/textAppearanceLarge"
                    android:text="@string/lbl_password"
                    android:id="@+id/tvPassword"
                    android:layout_marginTop="@dimen/activity_vertical_margin"/>

                <EditText
                    android:id="@+id/txtPassword"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:inputType="textPassword"
                    android:layout_marginLeft="@dimen/activity_horizontal_margin"
                    android:ems="10"
                    android:text="admin"/>

            </LinearLayout>
        </ScrollView>

        <FrameLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content">

            <LinearLayout
                android:id="@+id/linearLayout14"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal"
                android:gravity="bottom|center"
                android:weightSum="2"
                android:layout_gravity="bottom">

                <Button
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="@string/btn_begin"
                    android:id="@+id/btnLogin"
                    android:background="@android:color/transparent"
                    android:layout_weight="1"
                    android:textColor="@android:color/primary_text_dark"
                    android:paddingTop="@dimen/activity_vertical_margin"
                    android:paddingBottom="@dimen/activity_vertical_margin"
                    android:onClick="onClick"/>

                <Button
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="@string/btn_clear"
                    android:id="@+id/button7"
                    android:background="@android:color/transparent"
                    android:layout_weight="1"
                    android:textColor="@android:color/primary_text_dark"
                    android:paddingTop="@dimen/activity_vertical_margin"
                    android:paddingBottom="@dimen/activity_vertical_margin"/>
            </LinearLayout>
        </FrameLayout>

    </LinearLayout>

</RelativeLayout>




Devices | logcat

03-03 11:14:37.386    1891-1891/com.kjackson324.pikcms D/AndroidRuntime﹕ Shutting down VM
03-03 11:14:37.386    1891-1891/com.kjackson324.pikcms W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0x40018560)
03-03 11:14:37.413    1891-1891/com.kjackson324.pikcms E/AndroidRuntime﹕ FATAL EXCEPTION: main
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.kjackson324.pikcms/com.kjackson324.pikcms.Login}: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1696)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1716)
            at android.app.ActivityThread.access$1500(ActivityThread.java:124)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:968)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:130)
            at android.app.ActivityThread.main(ActivityThread.java:3806)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:507)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
            at android.support.v7.app.ActionBarActivityDelegate.onCreate(ActionBarActivityDelegate.java:151)
            at android.support.v7.app.ActionBarActivityDelegateBase.onCreate(ActionBarActivityDelegateBase.java:138)
            at android.support.v7.app.ActionBarActivity.onCreate(ActionBarActivity.java:123)
            at com.kjackson324.pikcms.Login.onCreate(Login.java:38)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1660)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1716)
            at android.app.ActivityThread.access$1500(ActivityThread.java:124)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:968)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:130)
            at android.app.ActivityThread.main(ActivityThread.java:3806)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:507)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
            at dalvik.system.NativeStart.main(Native Method)

1条回答
家丑人穷心不美
2楼-- · 2019-07-21 01:39

You need to replace your theme in the application tag of your manifest:

<application android:theme="@style/Theme.AppCompat">

ThemeOverlay should only be used with an actionBar with the new actionBarTheme attribute or with a toolbar. More informations here: https://chris.banes.me/2014/11/12/theme-vs-style/

查看更多
登录 后发表回答