java.lang.IllegalStateException应用程序立即崩溃

时间:2016-06-28 15:00:34

标签: java android xml

这是我的MainActivity.java类:

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文件一起粘贴到另一个项目中,一切正常。

我尝试了thisthisthat

中的前几个答案

但没有一个答案奏效。

非常感谢任何帮助!

哦,这是清单文件:

<?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>

1 个答案:

答案 0 :(得分:1)

问题在错误消息中完美描述:

  

您需要使用Theme.AppCompat主题(或后代)   活性。

由于您的MainActivityAppCompatActivity,因此您需要确保为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 {
    // ...
}