[英]Display dialog - IllegalStateException: You need to use a Theme.AppCompat theme
I want to display an AlertDialog, when the dialog should be appear I get the next message:我想显示一个 AlertDialog,当对话框应该出现时,我收到下一条消息:
IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
IllegalStateException:您需要在此活动中使用 Theme.AppCompat 主题(或后代)。
This is the code i'm using to display the dialog:这是我用来显示对话框的代码:
new AlertDialog.Builder(getBaseContext())
.setTitle("Not set yet :(")
.setMessage("You haven't set the directories to search in... Do you want to set it now?")
.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Intent i = new Intent(MainActivity.this, SettingsActivity.class);
startActivity(i);
}
})
.setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
}
})
.show();
This is the MainActivity headline:这是 MainActivity 的标题:
public class MainActivity extends AppCompatActivity
This is my styles.xml这是我的styles.xml
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
<style name="AppTheme.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
<style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />
<style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />
</resources>
I've tried to looking for answers over the internet and couldn't find an answer, how do I fix this problem?我试图通过互联网寻找答案但找不到答案,我该如何解决这个问题? I need an ActionBar, do I need to change the inheritance?
我需要一个ActionBar,我需要改变继承吗? Or should I change something in the AndroidManifest.xml or in styles.xml?
或者我应该在 AndroidManifest.xml 或 style.xml 中更改某些内容?
Thanks!谢谢!
Add this in your style.xml在你的style.xml 中添加这个
<style name="AlertDialogCustom" parent="Theme.AppCompat.Light.Dialog.Alert">
<item name="colorAccent">@color/colorAccent</item>
<item name="android:textColorPrimary">@color/colorPrimary</item>
<item name="android:background">#FFFFFF</item>
</style>
And use this for show Dialog (exit dialog example) in your MainActivity.java :并将其用于MainActivity.java 中的show Dialog(退出对话框示例):
AlertDialog.Builder builder = new AlertDialog.Builder(YourMainActivity.this, R.style.AlertDialogCustom);
builder.setTitle(getResources().getString(R.string.title));
builder.setMessage(getResources().getString(R.string.exit_dialog));
builder.setPositiveButton(getResources().getString(R.string.ok_dialog), new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
YourMainActivity.this.finish();
}
});
builder.setNegativeButton(getResources().getString(R.string.cancel_dialog), null);
builder.show();
Than in string.xml :比在string.xml 中:
<string name="cancel_dialog">CANCEL</string>
<string name="ok_dialog">OK</string>
<string name="exit_dialog">Are you sure you want to quit?</string>
<string name="title">Your App Title</string>
In colors.xml (optional):在colors.xml (可选)中:
<item name="colorAccent" type="color">#FFFF8800</item>
<item name="colorPrimary" type="color">#FFCC0000</item>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.