Android Firebase java.lang.IllegalStateException:此活动需要使用Theme.AppCompat主题(或子代)
我读了很多类似的问题,但不知道如何解决我的问题 我得到了这个类,我从firebase接收云消息通知:Android Firebase java.lang.IllegalStateException:此活动需要使用Theme.AppCompat主题(或子代),android,android-alertdialog,Android,Android Alertdialog,我读了很多类似的问题,但不知道如何解决我的问题 我得到了这个类,我从firebase接收云消息通知: public class FirebaseMessagingServiceImpl extends FirebaseMessagingService { private final static String TAG = "FirebaseMessaging"; @Override public void onMessageReceived(RemoteMessage
public class FirebaseMessagingServiceImpl extends FirebaseMessagingService {
private final static String TAG = "FirebaseMessaging";
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
Log.i(TAG, "Message from " + remoteMessage.getFrom());
if (remoteMessage.getNotification() != null) {
final String content = remoteMessage.getNotification().getBody();
final String title = remoteMessage.getNotification().getTitle();
Log.i(TAG, "...with content: " + content);
new Handler(Looper.getMainLooper()).post(new Runnable() {
@Override
public void run() {
UserInterfaceUtils.getAlertDialog(
getApplicationContext(),
title,
content,
android.R.string.ok,
android.R.string.no,
android.R.drawable.ic_dialog_alert,
null, null,
true, false).show();
}
});
}
super.onMessageReceived(remoteMessage);
}
@Override
public void onDeletedMessages() {
super.onDeletedMessages();
}
}
我想从那里创建一个警报对话框
。但是我得到了这个非法州例外。当我记录getApplicationContext()
的字符串时,我得到以下信息:android.support.multidex。MultiDexApplication@42585d08
我被困在这里,不知道如何解决这个问题
为完整起见,请参见我的getAlertDialog方法:
public static AlertDialog.Builder getAlertDialog(Context context, String title, String message, int pos, int neg, int icon, final Callable posFunc, final Callable negFunc, boolean posButton, boolean negButton) {
AlertDialog.Builder builder;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
builder = new AlertDialog.Builder(context, R.style.ShopDialogTheme);
} else {
builder = new AlertDialog.Builder(context);
}
builder.setTitle(title)
.setMessage(Html.fromHtml(message));
if (posButton)
builder.setPositiveButton(pos, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
try {
posFunc.call();
} catch (Exception e) {
e.printStackTrace();
}
}
});
if (negButton)
builder.setNegativeButton(neg, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
try {
negFunc.call();
} catch (Exception e) {
e.printStackTrace();
}
}
});
builder.setCancelable(false);
builder.setIcon(icon);
return builder;
}
根据标题,听起来代码运行时的任何活动都是扩展活动,它需要扩展AppCompatActivity,而活动使用的res/styles.xml中的任何主题都应该是AppCompat主题。这是清单中定义的服务。所以它没有主题。是的,但是你是从活动中调用服务的吗?不,我不是。您必须有一个正在运行的活动,我猜应用程序上下文可能正在使用该活动中的主题。因此,正在运行的任何活动都可能不是AppCompatActivity。发生错误时正在运行的活动是AppCompatActivity。