java.lang.IllegalStateException应用程序崩溃马上
问题描述:
这里是我的MainActivity.java类:java.lang.IllegalStateException应用程序崩溃马上
package com.example.roman.realestatecalculator;
import android.content.Intent;
import android.support.design.widget.TextInputEditText;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.GridView;
public class MainActivity extends AppCompatActivity {
Button propertyVet;
CheckBox dontKnow;
Button currentProperties;
DrawerLayout mDrawerLayout;
GridView mAppsGrid;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
dontKnow = (CheckBox) findViewById(R.id.dontKnow);
propertyVet = (Button) findViewById(R.id.propertyVet);
propertyVet.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent calc = new Intent(getApplicationContext(), Calculator.class);
startActivity(calc);
}
});
}
}
错误消息是在这里:
06-28 10:47:30.811 32734-32734/com.roman.realestatecalculator E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.roman.realestatecalculator, PID: 32734
java.lang.RuntimeException: Unable to start activity
ComponentInfo{com.roman.realestatecalculator/com.example.roman.realestatecalculator.MainActivity}:
java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3254)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3350)
at android.app.ActivityThread.access$1100(ActivityThread.java:222)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1795)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:158)
at android.app.ActivityThread.main(ActivityThread.java:7229)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
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:340)
at android.support.v7.app.AppCompatDelegateImplV7.ensureSubDecor(AppCompatDelegateImplV7.java:309)
at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:273)
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:136)
at com.example.roman.realestatecalculator.MainActivity.onCreate(MainActivity.java:32)
at android.app.Activity.performCreate(Activity.java:6876)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1135)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3207)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3350)
at android.app.ActivityThread.access$1100(ActivityThread.java:222)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1795)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:158)
at android.app.ActivityThread.main(ActivityThread.java:7229)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
这是说,错误是在第32行这是在这里:
setContentView(R.layout.activity_main);
但这似乎不大可能,因为我已粘贴此MainActivity类与xml文件进入不同的项目,一切正常。
试图排在前几位的答案,但没有一个答案工作。
任何帮助将不胜感激!
哦,这里是清单文件:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.roman.realestatecalculator">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:versionCode="3"
android:versionName="1.3">
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".Calculator" />
<activity android:name=".Expenses" />
<activity android:name=".Results" />
</application>
</manifest>
答
问题是在错误信息完全描述:
您需要使用Theme.AppCompat主题(或后代)与这 活动。
由于您的MainActivity
是AppCompatActivity
,你需要确保你使用的是一个Theme.AppCompat
主题为Activity
。
例如:
<activity android:name=".MainActivity"
android:theme="@style/Theme.AppCompat.Light">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
,或者根据您想支持的最低API级别,你可以只让MainActivity
扩展Activity
,而不是AppCompatActivity
:
public class MainActivity extends Activity {
// ...
}
请后的变化当你在[第一个链接]中尝试了一些最佳答案时,你对主题做出了回答(http://*.com/questions/21814825/you-need-to-use-a-theme-appcompat-theme-or -descendant与 - 此活动)。另外,您在发布的清单中有错误。 '''versionName'行末尾有一个额外的'>'。 –
你能分享你的styles.xml吗? – antonio
'android:versionName =“1.3”>(Manifest)中的尾随'>'是否是拼写错误? – antonio