[英]How to resolve: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity?
There are some other articles with same subject, but my case is different.还有一些其他文章主题相同,但我的情况不同。
I am using a BroadcastReciever in an AlarmManager application.我在AlarmManager应用程序中使用BroadcastReciever 。 Upon Alarm fire, I am trying to display an AlertDialog with custom layout.发生警报时,我试图显示具有自定义布局的AlertDialog 。 As the alarm gets fired, I get the following exception:当警报被触发时,我收到以下异常:
java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
Although I am using Theme.AppCompat.Light.NoActionBar
for my whole application.虽然我在整个应用程序中使用Theme.AppCompat.Light.NoActionBar
。 Here is the Manifest:
这是Manifest:
<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"
>
I am specifying this style in my dialog's layout file as well:我也在对话框的布局文件中指定了这种样式:
<?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>
Furthermore I am opening the dialog
form the onRecive()
method of my BroadcastReciver
class:此外,我正在打开我的BroadcastReciver
类的onRecive()
方法的dialog
:
@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();
}
NOTE: The onRecieve()
is getting called successfully, but the problem is with displaying dialog .注意: onRecieve()
被成功调用,但问题在于显示dialog 。 Before I also tried a separate activity
for displaying the dialog but the activity does not start from background when the app is closed.之前我还尝试了一个单独的activity
来显示对话框,但当应用程序关闭时,该活动不会从后台启动。
How can I resolve this?我该如何解决这个问题? Thanks in advance!提前致谢!
Broadcast receiver is the component of your application just like your Activity.广播接收器是您的应用程序的组件,就像您的活动一样。 You are trying to show Alert Dialog in receiver which required Theme.AppCompat, but your Broadcast receiver doesn't have that theme.您试图在需要 Theme.AppCompat 的接收器中显示警报对话框,但您的广播接收器没有该主题。 What you need to do is open an activity on receiving broadcast and show dialog in it like the calling feature.您需要做的是在接收广播时打开一个活动,并在其中显示对话,就像呼叫功能一样。 OR use notification manager to show notification with Action buttons.或使用通知管理器显示带有操作按钮的通知。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.