Android Studio 3.4
public class SubscribesFragment extends SubscribesBaseFragment {
@Override
public void onAttach(Context context) {
super.onAttach(context);
if (context instanceof Activity) {
mActivity = (Activity) context;
}
}
private BroadcastReceiver mMessageReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, final Intent intent) {
// Get extra data included in the Intent
String action = intent.getAction();
if (action.equals(ExistSubscribeWidget.ACTION_REFUSE_PAPER_INVOICE)) {
String providerGUID = intent.getStringExtra(ProviderWidget.EXTRA_PROVIDER_TAG);
String notificationSubscribeOwnerKey = intent.getStringExtra(ExistSubscribeWidget.NOTIFICATION_SUBSCRIBE_OWNERkEY);
showConfirmDialogRefusePaperInvoice(context, providerGUID, notificationSubscribeOwnerKey);
}
}
};
private void showConfirmDialogRefusePaperInvoice(Context context, final String providerGUID, final String notificationSubscribeOwnerKey) {
final Activity actitivity = mActivity;
AlertDialog.Builder builder = new AlertDialog.Builder(actitivity);
builder.setTitle(R.string.refuse_paper_dialog_title);
View customView = AndroidUtil.getLinearLayout(actitivity, R.layout.refuse_paper_dialog);
builder.setView(customView);
builder.setPositiveButton(R.string.ok, new android.content.DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//some code
}
});
builder.setNegativeButton(R.string.cancel, new android.content.DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
AlertDialog dialog = builder.create();
dialog.setCanceledOnTouchOutside(true);
dialog.show(); // crash here
}
}
但应用程序崩溃:
FATAL EXCEPTION: main
Process: com.myproject.app, PID: 6376
android.view.WindowManager$BadTokenException: Unable to add window -- token android.os.BinderProxy@292fbfb is not valid; is your activity running?
at android.view.ViewRootImpl.setView(ViewRootImpl.java:567)
at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:310)
at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:85)
at android.app.Dialog.show(Dialog.java:319)
at com.myproject.app.fragment.SubscribesFragment.showConfirmDialogRefusePaperInvoice(SubscribesFragment.java:108)
at com.myproject.app.fragment.SubscribesFragment.access$000(SubscribesFragment.java:38)
at com.myproject.app.fragment.SubscribesFragment$1.onReceive(SubscribesFragment.java:60)
at android.support.v4.content.LocalBroadcastManager.executePendingBroadcasts(LocalBroadcastManager.java:313)
at android.support.v4.content.LocalBroadcastManager$1.handleMessage(LocalBroadcastManager.java:121)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
答案 0 :(得分:1)
您正在使用brodcast恢复上下文显示对话框。为此使用活动上下文。