添加google play服务后,您需要在此活动中使用Theme.AppCompat主题(或子代)



在我目前正在开发的应用程序中(这是我的第一个android应用程序),我使用android maven插件和maven android sdk部署程序。应用程序一直运行良好(使用ActionBarActivity的MainActivity已经正确显示),直到我添加了一个谷歌播放服务库。之后,我得到了以下异常(除了向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

我的pom.xml是:

<?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>

和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>

我的styles.xml(在值vXX中,我有类似的条目):

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

因此,基本上添加这两个依赖项会导致"您需要使用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> 

你知道造成上述问题的原因是什么吗?如有任何帮助,我们将不胜感激。

我遇到了同样的问题,我在AndroidManifest.xml中通过从切换来修复它

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

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

我从以下问题的答案中得到了这个提示。

问题已解决。我已经将pom.xml中的部分更改为如下所示。

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

我还将谷歌播放服务的依赖关系更改为aar:

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

您应该使用这个:

android:theme="@style/Theme.AppCompat.Light.NoActionBar"

清单中的内部活动。祝你好运

尝试更改:

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

至:

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

相关内容

  • 没有找到相关文章

最新更新