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

问题描述:

我一直在试图获得一个对话框消息工作在android和应用程序总是崩溃时,它到达“dialog.show();”为什么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(); 
} 
} 

这里是我的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) 
+0

发布您的logcat! –

+2

我想你不能从广播接收器上下文启动UI。我会考虑启动一个显示对话框的活动 –

不能从接收器创建对话框。 创建对话框只能从UI组件(它有循环)。

您可以通过对话框开始透明活动,这对用户来说是一样的。

+0

谢谢,我会尽力做到这一点 – freedom666

使用BroadcastReceivercontext不能创建一个dialog,你有选项来解决这个问题:

第一个是创建具有唯一的大小经常活动对话框作为所示部分和全其余是透明:

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

第二个是保持你的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);