java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity异常的处理:
异常出现的过程:
使用android:theme="@android:style/Theme.DeviceDefault.Dialog"来改变程序的主题
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.jxust.elts"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="21" />
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.jxust.elts.activity.LoginActivity"
android:label="@string/app_name"
android:theme="@android:style/Theme.DeviceDefault.Dialog" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
一运行就报了以下的异常:
<span style="font-size:14px;">07-04 07:43:07.200: I/Choreographer(2169): Skipped 37 frames! The application may be doing too much work on its main thread.
07-04 07:49:05.660: E/AndroidRuntime(2316): Process: com.jxust.elts, PID: 2316
07-04 07:49:05.660: E/AndroidRuntime(2316): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.jxust.elts/com.jxust.elts.activity.LoginActivity}: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
07-04 07:49:05.660: E/AndroidRuntime(2316): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)
07-04 07:49:05.660: E/AndroidRuntime(2316): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)</span>
异常出现的原因:
package com.jxust.elts.activity;
import android.support.v7.app.ActionBarActivity;
import com.jxust.elts.R;
import com.jxust.elts.R.id;
import com.jxust.elts.R.layout;
import com.jxust.elts.R.menu;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
public class LoginActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is
// present.
getMenuInflater().inflate(R.menu.login, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
在LoginActivity.java中继承了ActionBarActivity就导致了这个异常的发生
解决办法:
将继承ActionBarActivity改为继承Activity即可解决这个问题
最后的效果: