显示对话框 - IllegalStateException异常:您需要使用Theme.AppCompat主题 [英] Display dialog - IllegalStateException: You need to use a Theme.AppCompat theme
问题描述
我要显示一个AlertDialog,当对话框应该会出现,我得到了一个消息:
I want to display an AlertDialog, when the dialog should be appear I get the next message:
IllegalStateException异常:您需要使用Theme.AppCompat主题(或
后裔)这个活动。
IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
这是code我使用显示对话框:
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();
这是在MainActivity标题:
This is the MainActivity headline:
public class MainActivity extends AppCompatActivity
这是我的styles.xml
This is my 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>
我试图寻找在互联网上的答案,找不到答案,
我该如何解决这个问题?
我需要一个动作条,我是否需要更改继承?或者我应该在AndroidManifest.xml或styles.xml改变些什么?
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? Or should I change something in the AndroidManifest.xml or in styles.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>
而在使用这个作秀对话框(退出对话框的例子)您的 MainActivity.java
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();
比 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>
在 colors.xml (可选):
<item name="colorAccent" type="color">#FFFF8800</item>
<item name="colorPrimary" type="color">#FFCC0000</item>
这篇关于显示对话框 - IllegalStateException异常:您需要使用Theme.AppCompat主题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!