Android:调用 AppCompatActivity 时抛出 IllegalStateException

Android: IllegalStateException is thrown while calling AppCompatActivity

如标题所述,我的应用程序退出并显示 IllegalStateException。我正在学习 Android 编程,所以您的帮助将非常有帮助。

根据LogCat,在MainActivity.java的onCreate方法中捕获异常如下。

public class MainActivity extends AppCompatActivity {
    private SQLiteHandler db;
    private SessionManager session;
    private boolean backPressedOnce = false;
    private FragmentManager manager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        manager = getFragmentManager();
        FragmentTransaction transaction = manager.beginTransaction();
        MainActivityFragment fragment = new MainActivityFragment();
        transaction.replace(R.id.fragment, fragment);
        transaction.addToBackStack(null);
        transaction.commit();

        db = new SQLiteHandler(getApplicationContext());
        session = new SessionManager(getApplicationContext());
        if(!session.isLoggedIn()) {
            logout();
        }
    }

setContentView 方法似乎有问题,让我进一步 post 代码的其他部分。

我认为这个问题是基于主题的,所以我想用 Androidmanifest.xml 文件询问你的意见。

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

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />

    <application
        android:name="com.marshall.politicalcompass.app.AppController"
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:theme="@android:style/Theme.Holo.Light.DarkActionBar" >
        <activity
            android:name=".SplashActivity"
            android:noHistory="true" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

        <activity android:name=".InitialActivity" />
        <activity android:name=".LoginActivity" android:noHistory="true"/>
        <activity android:name=".RegisterActivity" />
        <activity android:name=".MainActivity" android:launchMode="singleTop"/>
        <activity android:name=".TestActivity"
            android:label="@string/title_test"
            android:parentActivityName=".MainActivity"
            android:uiOptions="splitActionBarWhenNarrow" >
        <meta-data android:name="android.support.PARENT_ACTIVITY"
            android:value=".MainActivity" />
        </activity>
    </application>

</manifest>

最后是错误日志。

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.marshall.politicalcompass/com.marshall.politicalcompass.MainActivity}: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2413)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2471)
            at android.app.ActivityThread.access0(ActivityThread.java:175)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1308)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:146)
            at android.app.ActivityThread.main(ActivityThread.java:5602)
            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:1283)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
            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.AppCompatDelegateImplV7.createSubDecor(AppCompatDelegateImplV7.java:309)
            at android.support.v7.app.AppCompatDelegateImplV7.ensureSubDecor(AppCompatDelegateImplV7.java:278)
            at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:252)
            at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:109)
            at com.marshall.politicalcompass.MainActivity.onCreate(MainActivity.java:25)
            at android.app.Activity.performCreate(Activity.java:5451)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1093)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2377)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2471)
            at android.app.ActivityThread.access0(ActivityThread.java:175)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1308)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:146)
            at android.app.ActivityThread.main(ActivityThread.java:5602)
            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:1283)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
            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.

很明显。由于您不是从 Activity 继承,而是从 AppCompatActivity 继承。尝试将 activity 更改为: public class MainActivity extends Activity

否则,您必须在清单文件中定义适当的主题。

要使用 AppCompatActivity,您需要使用 AppCompat 主题。

在您的 AndroidManifest 更改中 android:theme="@android:style/Theme.Holo.Light.DarkActionBar"
android:theme="@style/Theme.AppCompat"