Android:调用AppCompatActivity时抛出IllegalStateException

时间:2015-09-08 18:56:06

标签: java android illegalstateexception 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方法似乎有问题,所以让我进一步发布代码的其他部分。

我认为这个问题是基于主题的,所以我想用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.access$900(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.access$900(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)

如果您需要更多信息,请告诉我。

2 个答案:

答案 0 :(得分:1)

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

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

答案 1 :(得分:0)

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

非常明显。由于您不是从Activity继承,而是从AppCompatActivity继承。尝试将您的活动更改为: public class MainActivity extends Activity

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