得到“您需要在此活动中使用Theme.AppCompat主题(或后代)."试图显示来自BroadcastReceiver的AlertDialog时 [英] Getting 'You need to use a Theme.AppCompat theme (or descendant) with this activity.' while trying to show an AlertDialog from BroadcastReceiver
问题描述
我正在从服务发送Broadcast
,然后从内部BroadcastReceiver
类接收回它.我必须显示基于某些逻辑的AlertDialog
,但是在尝试执行此操作时,出现此运行时错误:java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
I'm sending a Broadcast
from a service and then receiving it back from inner BroadcastReceiver
class. I have to show an AlertDialog
based on some logic but while trying to do it, I'm getting this runtime error: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
这是MyBroadcastReceiver
类:
public class MyBroadcastReceiver extends BroadcastReceiver {
public MyBroadcastReceiver(){
super();
}
@Override public void onReceive(final Context context, Intent intent) {
if (intent.getAction() != null && intent.getAction().equals(getString(R.string.broadcast_id))) {
Intent intent1 = new Intent(MyService.this, MainActivity.class);
intent1.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent1);
@Override
public void run() {
if (someCondition) {
android.support.v7.app.AlertDialog.Builder builder1 = new android.support.v7.app.AlertDialog.Builder(getBaseContext());
builder1.setView(R.layout.dialog);
builder1.setPositiveButton(
"OK",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
Intent notificationIntent = new Intent(getBaseContext(), Notification.class);
notificationIntent.putExtra(Notification.NOTIFICATION, getNotificationGame());
PendingIntent pendingIntent = PendingIntent.getBroadcast(getBaseContext(), m, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager = (AlarmManager) getBaseContext().getSystemService(Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, finalFutureInMillis, pendingIntent);
postMethod(MainActivity.name, MainActivity.uidOfProfilePic, String.valueOf(MainActivity.currentLatDouble), String.valueOf(MainActivity.currentLngDouble), null, s, v, sA, requestID, user.getUid(), nP);
dialog.cancel();
}
});
final android.support.v7.app.AlertDialog alert11 = builder1.create();
alert11.setCancelable(false);
alert11.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
// error on line below
alert11.show();
}
}
}
}
}
I searched the internet for this and came up with this which suggests to create an activity and do the AlertDialog code in it, but as you can see that I'm doing some work when user clicks OK
and that code retrieved in the Service
and I will be unable to access it from another activity.
请让我知道,我现在应该做什么?
Please let me know what should I do now?
推荐答案
Hammad,文档中说您不能在onReceive()的实现中启动弹出对话框. (如答案说的那样) https://stackoverflow.com/a/4855397/7542998
Hammad, the documentation say that you cannot launch a popup dialog in your implementation of onReceive(). (like the answers say it) https://stackoverflow.com/a/4855397/7542998
认为接收方可以接收并启动新流程.我说这是因为在几秒钟的数据处理之后,接收器可能会被系统阻塞,因此在对它进行一些逻辑处理时,请始终牢记这一点.
The Receivers are thought for receive and initiate new process. I say that because after a fews seconds of data processing the receiver can be blocked by the system, so take always that in mind when you put some logic on it.
但是,喜欢说另一个答案
However, like say this other answer
( https://stackoverflow.com/a/16332260/7542998 ),您可以尝试使用NotificationManager API.
(https://stackoverflow.com/a/16332260/7542998) you can try with NotificationManager API.
在两个链接中都有实现您想要的好答案.
In both links there are good answers to achieve what you want.
希望这会有所帮助.
这篇关于得到“您需要在此活动中使用Theme.AppCompat主题(或后代)."试图显示来自BroadcastReceiver的AlertDialog时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!