简体   繁体   English

为什么dialog.show()导致我的应用崩溃?

[英]Why is dialog.show() causing my app to crash?

I've been trying to get a dialog message to work on android and the app always crash when it reaches "dialog.show();" 我一直在尝试获取一条对话框消息,以便在android上运行,并且该应用在到达“ dialog.show();”时总是崩溃

public class Logic extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {

    AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(context);
    dialogBuilder.setTitle("Alarm");
    dialogBuilder.setMessage(messageActivity.getMes());
    dialogBuilder.setPositiveButton("OK",
            new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dialog.dismiss();
                }
            });
    AlertDialog dialog = dialogBuilder.create();
    dialog.show();
}
}

Here is my logcat: 这是我的日志:

FATAL EXCEPTION: main

Process: it226.myapplicationit226androidapp, PID: 19598
java.lang.RuntimeException: Unable to start receiver it226.myapplicationit226androidapp.Logic: android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
    at android.app.ActivityThread.handleReceiver(ActivityThread.java:2732)
    at android.app.ActivityThread.-wrap14(ActivityThread.java)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1421)
    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)
 Caused by: android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
    at android.view.ViewRootImpl.setView(ViewRootImpl.java:571)
    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 it226.myapplicationit226androidapp.Logic.onReceive(Logic.java:65)
    at android.app.ActivityThread.handleReceiver(ActivityThread.java:2725)
    at android.app.ActivityThread.-wrap14(ActivityThread.java) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1421) 
    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) 

You can't create dialog from receiver. 您无法从接收方创建对话框。 Creating dialogs is allowed only from UI components (which have looper). 仅允许从UI组件(具有循环器)创建对话框。

You could start transparent activity with dialog, which would be the same for user. 您可以从对话框开始透明活动,这与用户相同。

You can't create a dialog using the context of a BroadcastReceiver , you has two options to solve this problem: 您不能使用BroadcastReceivercontext创建dialog ,但是有两个选项可以解决此问题:

first one is to create a regular Activity which has only the size of the dialog as a shown part and the full remaining is transparent: 第一个是创建一个常规活动,该活动仅具有对话框的大小,如图所示,其余全部是透明的:

Intent newIntent = new Intent(context, Displayer.class);
newIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(newIntent);

second one is to hold your Activity context and then use it in creating the dialog, but at this method you have to make sure what is the activity that is currently open: 第二个是保留您的Activity上下文,然后在创建对话框时使用它,但是在这种方法下,您必须确保当前打开的是什么活动:

// in your activity onCreate
ctx = YourActivity.this; // let's suppose ctx is static and general var
////////////////////////////////////////////////
// in the BroadcastReceiver
AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(YourActivity.ctx);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM