Android : 在来自助手 class 的任何可见 activity 上调用警报对话框?
Android : Invoke the Alert dialog on any visible activity from helper class?
我有以下问题。
我试图从简单的帮助程序调用警报对话框 class
public class NotificationHelper
因为代码不是在activity上执行的,而是在收到推送通知后执行的,如果activity在前台运行。
所以我尝试通过这种方式来做:
// Show dialog
Handler handler = new Handler(Looper.getMainLooper());
final String finalNotificationHeading = notificationHeading;
final String finalNotificationBody = notificationBody;
handler.post(
new Runnable() {
@Override
public void run() {
DialogHelper.showSimpleAlert(finalNotificationHeading, finalNotificationBody, ctx.getString(R.string.positive_button_text),
ctx.getString(R.string.negative_button_text), null, ctx, notificationAlertCallback);
}
}
);
但我总是收到以下错误:
com.afollestad.materialdialogs.MaterialDialog$DialogException: Bad window token, you cannot show a dialog before an Activity is created or after it's hidden.
注意:错误不是由插件引起的。
请问如何解决?
非常感谢您的任何建议。
你的问题是ctx。还有你经过它的那一刻。
它应该在您想要创建对话框时给出,并且应该是当前 UI(activity、片段等)
Bad window token
说您尝试添加对话框的上下文当前不在屏幕上。
所以你的方法应该是这样的
NotificationHelper{
void showDialog(Activity currentActivity){
Handler handler = new Handler(Looper.getMainLooper());
final String finalNotificationHeading = notificationHeading;
final String finalNotificationBody = notificationBody;
handler.post(
new Runnable() {
@Override
public void run() {
DialogHelper.showSimpleAlert(finalNotificationHeading, finalNotificationBody, currentActivity.getString(R.string.positive_button_text),
currentActivity.getString(R.string.negative_button_text), null, currentActivity, notificationAlertCallback);
}
}
);
}
如果您不想在调用 showDialog 时一直传递 currentActivity,您应该在要在
中使用它的每个 Activity 的 onResume 中传递 currentContext
public void onResume(){
super.onResume();
NotificationHelper.ctx = this;
}
并像您的情况一样使用它,但您必须意识到,如果您传递了错误的上下文(或根本不这样做),则可能会出现错误的 window 令牌异常。
我有以下问题。
我试图从简单的帮助程序调用警报对话框 class
public class NotificationHelper
因为代码不是在activity上执行的,而是在收到推送通知后执行的,如果activity在前台运行。
所以我尝试通过这种方式来做:
// Show dialog
Handler handler = new Handler(Looper.getMainLooper());
final String finalNotificationHeading = notificationHeading;
final String finalNotificationBody = notificationBody;
handler.post(
new Runnable() {
@Override
public void run() {
DialogHelper.showSimpleAlert(finalNotificationHeading, finalNotificationBody, ctx.getString(R.string.positive_button_text),
ctx.getString(R.string.negative_button_text), null, ctx, notificationAlertCallback);
}
}
);
但我总是收到以下错误:
com.afollestad.materialdialogs.MaterialDialog$DialogException: Bad window token, you cannot show a dialog before an Activity is created or after it's hidden.
注意:错误不是由插件引起的。
请问如何解决?
非常感谢您的任何建议。
你的问题是ctx。还有你经过它的那一刻。
它应该在您想要创建对话框时给出,并且应该是当前 UI(activity、片段等)
Bad window token
说您尝试添加对话框的上下文当前不在屏幕上。
所以你的方法应该是这样的
NotificationHelper{
void showDialog(Activity currentActivity){
Handler handler = new Handler(Looper.getMainLooper());
final String finalNotificationHeading = notificationHeading;
final String finalNotificationBody = notificationBody;
handler.post(
new Runnable() {
@Override
public void run() {
DialogHelper.showSimpleAlert(finalNotificationHeading, finalNotificationBody, currentActivity.getString(R.string.positive_button_text),
currentActivity.getString(R.string.negative_button_text), null, currentActivity, notificationAlertCallback);
}
}
);
}
如果您不想在调用 showDialog 时一直传递 currentActivity,您应该在要在
中使用它的每个 Activity 的 onResume 中传递 currentContextpublic void onResume(){
super.onResume();
NotificationHelper.ctx = this;
}
并像您的情况一样使用它,但您必须意识到,如果您传递了错误的上下文(或根本不这样做),则可能会出现错误的 window 令牌异常。