如标题所述,我的应用程序退出并显示 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)
如果您需要更多信息,请告诉我。