显示对话框 - IllegalStateException:您需要使用 Theme.AppCompat 主题
Display dialog - IllegalStateException: You need to use a Theme.AppCompat theme
我想显示一个 AlertDialog,当对话框应该出现时我收到下一条消息:
IllegalStateException: You need to use a Theme.AppCompat theme (or
descendant) with this activity.
这是我用来显示对话框的代码:
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 标题:
public class MainActivity extends AppCompatActivity
这是我的 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>
我曾尝试在互联网上寻找答案,但找不到答案,
我该如何解决这个问题?
我需要一个 ActionBar,我需要更改继承吗?或者我应该更改 AndroidManifest.xml 或 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>
我想显示一个 AlertDialog,当对话框应该出现时我收到下一条消息:
IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
这是我用来显示对话框的代码:
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 标题:
public class MainActivity extends AppCompatActivity
这是我的 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>
我曾尝试在互联网上寻找答案,但找不到答案, 我该如何解决这个问题? 我需要一个 ActionBar,我需要更改继承吗?或者我应该更改 AndroidManifest.xml 或 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>