【发布时间】:2016-05-16 06:35:49
【问题描述】:
我正在尝试在删除按钮上使用警报对话框。但是显示对话框显示异常。当我单击删除时,它会崩溃并在 .show 上显示异常。
我尝试为此活动使用 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();
}
});
我的主题
<!-- 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>
谢谢。
【问题讨论】:
-
我认为异常是 BadTokenException。您需要关闭 NO 和 DELETE 按钮上的警报对话框
-
按钮的onClick()?
-
setPositiveButton 和 setNegativeButton 的onClick
标签: java android android-appcompat android-theme illegalstateexception