使用AlertDialog报错 You need to use a Theme.AppCompat theme (or descendant) with this activity.之解决

以前由于继承自ActionBarActivity用得好好的,现在直接继承Activity,使用AlertDialog,则会报以下这个错误:

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

 

可以这样解决,在styles.xml中

添加

<style name=”AlertDialogCustom” parent=”Theme.AppCompat.Light.Dialog.Alert”>

<item name=”android:background”>#FFFFFF</item>
</style>

然后在代码中,这样处理

AlertDialog.Builder builder = new AlertDialog.Builder(
AcceptanceListActivity.this,
R.style.AlertDialogCustom);
builder.setTitle(“我是标题”);
builder.setMessage(“我是内容”);
builder.setPositiveButton(“确定”,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int whichButton) {

}
});
builder.show();

 

OK,解决了。