[英]java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity
I am trying to use alert dialog on delete button. 我试图在删除按钮上使用警报对话框。 But It is showing exception to show the dialog. 但是显示对话框显示异常。 When I click on delete it crashes and shows exception on .show. 当我单击删除时,它崩溃并在.show上显示异常。
I tried to use Theme.AppCompat theme for this activity but still it crashes. 我尝试将Theme.AppCompat主题用于此活动,但仍然崩溃。
<activity android:name=".AddEventActivity"
android:theme="@style/Theme.AppCompat">
</activity>
delete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new AlertDialog.Builder(getApplicationContext())
.setTitle("Delete entry")
.setMessage("Are you sure you want to delete this entry?")
.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(getApplicationContext(), "Yaay", Toast.LENGTH_SHORT).show();
i = new Intent();
db.deleteEvent(eventData);
Log.i("d", "delete");
setResult(RESULT_OK, i);
finish();
// continue with delete
}
})
.setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// do nothing
}
})
.setIcon(android.R.drawable.ic_dialog_alert)
.show();
}
});
My Theme 我的主题
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
<item name="windowActionBarOverlay">false</item>
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
<item name="android:windowBackground">@color/background_material_light</item>
</style>
<style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />
<style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />
</resources>
Thank you. 谢谢。
u are using the wrong context you have to use the activity context and not the application context 您使用了错误的上下文,您必须使用活动上下文而不是应用程序上下文
change: 更改:
new AlertDialog.Builder(getApplicationContext())
to 至
new AlertDialog.Builder(YourActivityName.this)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.