[android]对替换 AppCompatActivity 与强制关闭Activity

发布时间: 2016/6/29 20:49:00
注意事项: 本文中文内容可能为机器翻译,如要查看英文原文请点击上面连接.

我有一个旧的项目在 Eclipse 中,导入它入 Android studio来重建它与材料设计。 我做所有的事情......

配置的 build.gradle 和依赖项 ︰

apply plugin: 'com.android.application'

android {
    compileSdkVersion 22
    buildToolsVersion "22.0.1"

    defaultConfig {
        applicationId "com.mhrz.Minesweeper"
        minSdkVersion 8
        targetSdkVersion 22
        versionCode 1
        versionName "1.0"
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
        }
    }
}
dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:22.1.1'
    compile 'com.android.support:cardview-v7:+'
}

和样式 ︰

<resources>

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <item name="colorPrimary">#1E9618</item>
        <item name="colorPrimaryDark">#146310</item>
        <item name="colorAccent">#1E9618</item>
    </style>

</resources>

这是 Manifest :

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.mhrz.Minesweeper"
      android:versionCode="1"
      android:versionName="1.0">
    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".MinesweeperGame"
                  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> 

和也卡片视图-效果很好。

问题是,应用程序,正确配合着这和给我全息设计 ︰

public class MinesweeperGame extends Activity {

但我想要的材料和 FC 相处 ︰

public class MinesweeperGame extends AppCompatActivity {

log :

01-03 16:29:38.779  12720-12720/com.mhrz.Minesweeper E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.mhrz.Minesweeper, PID: 12720
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.mhrz.Minesweeper/com.mhrz.Minesweeper.MinesweeperGame}: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2219)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2269)
            at android.app.ActivityThread.access$800(ActivityThread.java:135)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5045)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
            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.AppCompatDelegateImplBase.onCreate(AppCompatDelegateImplBase.java:113)
            at android.support.v7.app.AppCompatDelegateImplV7.onCreate(AppCompatDelegateImplV7.java:146)
            at android.support.v7.app.AppCompatActivity.onCreate(AppCompatActivity.java:59)
            at com.mhrz.Minesweeper.MinesweeperGame.onCreate(MinesweeperGame.java:55)
            at android.app.Activity.performCreate(Activity.java:5231)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1104)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2163)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2269)
            at android.app.ActivityThread.access$800(ActivityThread.java:135)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5045)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
            at dalvik.system.NativeStart.main(Native Method)

错误在哪里?如何修复它?

解决方法 1:

Android系统并不知道你想要使用备用的主题 application (喜欢你 AppTheme ) 直到你定义它在 applicationactivity 属性内 AndroidManifest.xml 文件。

您需要添加 android:theme="@style/AppTheme" 归因于你 <application> tagAndroidManifest.xml 像这样 ︰

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.mhrz.Minesweeper"
      android:versionCode="1"
      android:versionName="1.0">
    <application android:icon="@drawable/icon"
                 android:label="@string/app_name"
                 android:theme="@style/AppTheme">
        <activity android:name=".MinesweeperGame"
                  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> 
官方微信
官方QQ群
31647020