Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

You need to use a Theme.AppCompat theme (or descendant) with this activity after adding google-play-services

In application I'm currently developing (it's my first android application) I use android-maven-plugin and maven-android-sdk-deployer. Application had been working great (MainActivity that uses ActionBarActivity had been shown correctly) until I added a google-play-servies library. After that I'm getting the following exception (no changes in application except adding required dependencies to pom.xml):

E/AndroidRuntime( 2146): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.myapp/com.myapp.acts.MainActivity}: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
E/AndroidRuntime( 2146):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)
E/AndroidRuntime( 2146):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
E/AndroidRuntime( 2146):    at android.app.ActivityThread.access$800(ActivityThread.java:144)
E/AndroidRuntime( 2146):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
E/AndroidRuntime( 2146):    at android.os.Handler.dispatchMessage(Handler.java:102)
E/AndroidRuntime( 2146):    at android.os.Looper.loop(Looper.java:135)
E/AndroidRuntime( 2146):    at android.app.ActivityThread.main(ActivityThread.java:5221)
E/AndroidRuntime( 2146):    at java.lang.reflect.Method.invoke(Native Method)
E/AndroidRuntime( 2146):    at java.lang.reflect.Method.invoke(Method.java:372)
E/AndroidRuntime( 2146):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
E/AndroidRuntime( 2146):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
E/AndroidRuntime( 2146): Caused by: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
E/AndroidRuntime( 2146):    at android.support.v7.app.ActionBarActivityDelegate.onCreate(ActionBarActivityDelegate.java:147)
E/AndroidRuntime( 2146):    at android.support.v7.app.ActionBarActivityDelegateBase.onCreate(ActionBarActivityDelegateBase.java:139)
E/AndroidRuntime( 2146):    at android.support.v7.app.ActionBarActivity.onCreate(ActionBarActivity.java:123)
E/AndroidRuntime( 2146):    at com.metova.roboguice.appcompat.RoboActionBarActivity.onCreate(RoboActionBarActivity.java:45)
E/AndroidRuntime( 2146):    at com.myapp.acts.BaseActionBarActivity.onCreate(BaseActionBarActivity.java:23)
E/AndroidRuntime( 2146):    at com.myapp.acts.MainActivity.onCreate(MainActivity.java:62)
E/AndroidRuntime( 2146):    at android.app.Activity.performCreate(Activity.java:5933)
E/AndroidRuntime( 2146):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
E/AndroidRuntime( 2146):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)
E/AndroidRuntime( 2146):    ... 10 more

My pom.xml is:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.myapp</groupId>
    <artifactId>myapp</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>apk</packaging>
    <name>myapp</name>

    <properties>
        <android.sdk.path>/data/tools/android-sdk-linux</android.sdk.path>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <platform.version>5.0_r1</platform.version>
        <android.plugin.version>3.8.2</android.plugin.version>
        <robospice.version>1.4.13</robospice.version>
        <picasso.version>2.3.5-SNAPSHOT</picasso.version>
        <android.support.version>21.0.1</android.support.version>
        <sdk.platform.version>21</sdk.platform.version>

        <commons.logging.version>1.1.3</commons.logging.version>
        <android-maven-plugin.version>3.7.0</android-maven-plugin.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>android.support</groupId>
            <artifactId>compatibility-v7-appcompat</artifactId>
            <version>${android.support.version}</version>
            <type>apklib</type>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>android.support</groupId>
            <artifactId>compatibility-v7-appcompat</artifactId>
            <version>${android.support.version}</version>
            <type>jar</type>
        </dependency>
        <dependency>
            <groupId>android.support</groupId>
            <artifactId>compatibility-v4</artifactId>
            <version>${android.support.version}</version>

        </dependency>
        <dependency>
            <groupId>org.roboguice</groupId>
            <artifactId>roboguice</artifactId>
            <version>2.0</version>
        </dependency>

        <dependency>
            <groupId>com.octo.android.robospice</groupId>
            <artifactId>robospice</artifactId>
            <version>${robospice.version}</version>
        </dependency>

        <dependency>
            <groupId>com.octo.android.robospice</groupId>
            <artifactId>robospice-retrofit</artifactId>
            <version>${robospice.version}</version>
        </dependency>

        <dependency>
            <groupId>commons-logging</groupId>
            <artifactId>commons-logging</artifactId>
            <version>${commons.logging.version}</version>
            <scope>provided</scope>
        </dependency>

        <dependency>
            <groupId>com.squareup</groupId>
            <artifactId>otto</artifactId>
            <version>1.3.5</version>
        </dependency>

        <dependency>
            <groupId>android</groupId>
            <artifactId>android</artifactId>
            <version>${platform.version}</version>
            <scope>provided</scope>
        </dependency>

        <dependency>
            <groupId>com.google.android.maps</groupId>
            <artifactId>maps</artifactId>
            <version>21_r1</version>
            <scope>provided</scope>
        </dependency>

        <dependency>
            <groupId>com.google.android.gms</groupId>
            <artifactId>google-play-services</artifactId>
            <version>21.0.0</version>
            <type>apklib</type>
        </dependency>

        <dependency>
            <groupId>com.google.android.gms</groupId>
            <artifactId>google-play-services</artifactId>
            <version>21.0.0</version>
            <type>jar</type>
        </dependency>

        <dependency>
            <groupId>com.squareup.okhttp</groupId>
            <artifactId>okhttp</artifactId>
            <version>2.0.0</version>
        </dependency>
        <dependency>
            <groupId>com.squareup.okhttp</groupId>
            <artifactId>okhttp-urlconnection</artifactId>
            <version>2.0.0</version>
        </dependency>

        <dependency>
            <groupId>com.squareup.picasso</groupId>
            <artifactId>picasso</artifactId>
            <version>${picasso.version}</version>
        </dependency>

        <dependency>
            <artifactId>crouton</artifactId>
            <groupId>de.keyboardsurfer.android.widget</groupId>
            <version>1.8.5</version>
            <exclusions>
                <exclusion>
                    <groupId>com.android.support</groupId>
                    <artifactId>support-v4</artifactId>

                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>com.google.inject</groupId>
            <artifactId>guice</artifactId>
            <classifier>no_aop</classifier>
            <version>4.0-beta</version>
        </dependency>
    </dependencies>
    <build>
        <finalName>${project.artifactId}</finalName>
        <pluginManagement>
            <plugins>
                <plugin>
                    <groupId>com.jayway.maven.plugins.android.generation2</groupId>
                    <artifactId>android-maven-plugin</artifactId>
                    <version>${android.plugin.version}</version>
                    <extensions>true</extensions>
                </plugin>

                <plugin>
                    <groupId>org.eclipse.m2e</groupId>
                    <artifactId>lifecycle-mapping</artifactId>
                    <version>1.0.0</version>
                    <configuration>
                        <lifecycleMappingMetadata>
                            <pluginExecutions>
                                <pluginExecution>
                                    <pluginExecutionFilter>
                                        <groupId>com.jayway.maven.plugins.android.generation2</groupId>
                                        <artifactId>android-maven-plugin</artifactId>
                                        <versionRange>[1.0.0,)</versionRange>
                                        <goals>
                                            <goal>consume-aar</goal>
                                        </goals>
                                    </pluginExecutionFilter>
                                    <action>
                                        <ignore />
                                    </action>
                                </pluginExecution>

                            </pluginExecutions>
                        </lifecycleMappingMetadata>
                    </configuration>
                </plugin>
            </plugins>
        </pluginManagement>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-enforcer-plugin</artifactId>
            </plugin>
            <plugin>
                <groupId>com.jayway.maven.plugins.android.generation2</groupId>
                <artifactId>android-maven-plugin</artifactId>
                <extensions>true</extensions>
                <configuration>
                    <sdk>
                        <platform>${sdk.platform.version}</platform>
                    </sdk>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.6</source>
                    <target>1.6</target>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

and AndroidManifest.xml:

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

    <uses-sdk
        android:minSdkVersion="10"
        android:targetSdkVersion="21" />

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

    <!-- maps -->
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />

    <uses-feature
        android:glEsVersion="0x00020000"
        android:required="true" />
    <!-- /maps -->
    <application
        android:allowBackup="true"
        android:icon="@drawable/icon"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <meta-data
            android:name="com.google.android.gms.version"
            android:value="@integer/google_play_services_version" />
        <meta-data
            android:name="com.google.android.maps.v2.API_KEY"
            android:value="MY_KEY" />

        <activity android:name=".acts.LoginActivity" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
        <activity
            android:name=".acts.RegisterActivity"
            android:label="@string/app_name" >
        </activity>
        <activity
            android:name=".acts.MainActivity"
            android:label="@string/app_name">
        </activity>
        <activity
            android:name=".acts.profiles.SearchProfilesActivity"
            android:label="@string/title_activity_search_profiles" >
            <meta-data
                android:name="android.support.PARENT_ACTIVITY"
                android:value=".acts.MainActivity" />
        </activity>

        <service
            android:name="com.myapp.net.RetrofitSpiceService"
            android:exported="false" />
    </application>

</manifest>

My styles.xml (in values-vXX I have simillar entries):

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

    <style name="AppTheme" parent="AppBaseTheme">
    </style>

</resources>

So basically adding these two dependencies causes application crash with "You need to use a Theme.AppCompat...":

       <dependency>
            <groupId>com.google.android.gms</groupId>
            <artifactId>google-play-services</artifactId>
            <version>21.0.0</version>
            <type>apklib</type>
        </dependency>

        <dependency>
            <groupId>com.google.android.gms</groupId>
            <artifactId>google-play-services</artifactId>
            <version>21.0.0</version>
            <type>jar</type>
        </dependency> 

Do you have any idea what is the cause of above problem? Any help would be appreciated.

like image 624
Arcaddius Avatar asked Nov 03 '14 21:11

Arcaddius


People also ask

Is it possible to add a theme to an Android activity?

If you have *an activity that doesn't need this support, such as you only care about the latest versions and features of Android, you can apply any theme to it but the java code must extend plain old Activity.

Why can't I apply the dialog theme to a specific activity?

See my answer. stackoverflow.com/a/51574281/232727 The reason you are having this problem is because the activity you are trying to apply the dialog theme to is extending ActionBarActivity which requires the AppCompat theme to be applied. Update: Extending AppCompatActivity would also have this problem

How to change the theme of action bar in Android manifest?

If you don't need advanced attributes about action bar or AppCompat you don't need to use Actionbar or AppCompat. In Android manifest just change theme of activity to AppTheme as follow code snippet <activity android:name=".MainActivity" android:label="@string/app_name" android:theme="@style/AppTheme"> </activity>

When to extend appcompatactivity in Android?

The general rule is that if you want your code to support older versions of Android, it should have the AppCompat theme and the java code should extend AppCompatActivity. If you have *an activity that doesn't need this support, such as you only care about the latest versions and features of Android,...


2 Answers

I had the same problem and I fixed it in the AndroidManifest.xml by switching from

<application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >

to

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

I got this hint from an answer to the following issue.

like image 105
gemue Avatar answered Sep 19 '22 11:09

gemue


Problem solved. I've changed section in pom.xml to this showed below.

<build>
        <finalName>${project.artifactId}</finalName>
        <pluginManagement>
            <plugins>
                <plugin>
                    <groupId>org.eclipse.m2e</groupId>
                    <artifactId>lifecycle-mapping</artifactId>
                    <version>${lifecycle-mapping-plugin.version}</version>
                    <configuration>
                        <lifecycleMappingMetadata>
                            <pluginExecutions>
                                <pluginExecution>
                                    <pluginExecutionFilter>
                                        <groupId>
                                            com.jayway.maven.plugins.android.generation2
                                        </groupId>
                                        <artifactId>
                                            android-maven-plugin
                                        </artifactId>
                                        <versionRange>
                                            [${android-maven-plugin.version},)
                                        </versionRange>
                                        <goals>
                                            <goal>consume-aar</goal>
                                        </goals>
                                    </pluginExecutionFilter>
                                    <action>
                                        <ignore />
                                    </action>
                                </pluginExecution>
                            </pluginExecutions>
                        </lifecycleMappingMetadata>
                    </configuration>
                </plugin>
            </plugins>
        </pluginManagement>

        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-enforcer-plugin</artifactId>
            </plugin>
            <plugin>
                <groupId>com.jayway.maven.plugins.android.generation2</groupId>
                <artifactId>android-maven-plugin</artifactId>
                <extensions>true</extensions>
                <configuration>
                    <androidManifestFile>${project.basedir}/AndroidManifest.xml</androidManifestFile>
                    <assetsDirectory>${project.basedir}/assets</assetsDirectory>
                    <resourceDirectory>${project.basedir}/res</resourceDirectory>
                    <nativeLibrariesDirectory>${project.basedir}/src/main/native</nativeLibrariesDirectory>
                    <sdk>
                        <platform>${sdk.platform.version}</platform>
                    </sdk>
                    <undeployBeforeDeploy>true</undeployBeforeDeploy>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.6</source>
                    <target>1.6</target>
                </configuration>
            </plugin>
        </plugins>
    </build>

I've also changed the dependency for google-play-services to aar:

<dependency>
    <groupId>com.google.android.gms</groupId>
    <artifactId>google-play-services</artifactId>
    <version>6.1.71</version>
    <type>aar</type>
</dependency>
like image 28
Arcaddius Avatar answered Sep 19 '22 11:09

Arcaddius