同样主题的文章还有一些,但我的情况不同。
我在 AlarmManager 应用程序中使用 BroadcastReciever。警报触发后,我试图显示具有自定义布局的 AlertDialog。当警报被触发时,我得到以下异常:
java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
虽然我在整个应用程序中使用 Theme.AppCompat.Light.NoActionBar
。
这是 list :
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.AppCompat.Light.NoActionBar"
>
我也在对话框的布局文件中指定了这种样式:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#10000000"
android:orientation="vertical"
style="@style/Theme.AppCompat.Light.NoActionBar"
android:padding="10dip" >
<TextView/>...
<Button/>...
</LinearLayout>
此外我正在从我的BroadcastReciver
类的onRecive()
方法中打开对话框
:
@Override
public void onReceive(Context context, Intent intent) {
LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View v = inflater.inflate(R.layout.fragment_alarm_running, null);
final AlertDialog dialog = new AlertDialog.Builder(context)
.setView(v)
.setCancelable(false)
.create();
dialog.setOnShowListener(new DialogInterface.OnShowListener() {
@Override
public void onShow(final DialogInterface dialog) { ... }
});
dialog.show();
}
注意 onRecieve()
被成功调用,但问题在于显示对话框。之前我还尝试了一个单独的 activity
来显示对话框,但是当应用程序关闭时该 Activity 不会从后台启动。
我该如何解决这个问题?提前致谢!
最佳答案
广播接收器就像您的 Activity 一样是应用程序的组件。 您正在尝试在需要 Theme.AppCompat 的接收器中显示警报对话框,但您的广播接收器没有该主题。 您需要做的是打开一个关于接收广播的 Activity ,并在其中显示对话框,例如调用功能。或者使用通知管理器显示带有操作按钮的通知。
关于java - 如何使用此 Activity 解决 : java. lang.IllegalStateException : You need to use a Theme. AppCompat 主题(或后代)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59352058/