Dialog使应用程序每隔一段时间崩溃(奇怪)

时间:2017-11-13 00:22:30

标签: android

我的班级发送了MainActivity广播。然后,我的MainActivity会显示一个AlertDialog,其中包含相应的字符串。这是代码 -

 @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    LocalBroadcastManager.getInstance(this).registerReceiver(keyReceiver, new IntentFilter("intentKey"));
}

private BroadcastReceiver keyReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        // get real-time status updates
        String status = intent.getStringExtra("status");
        if (status.equals("update")) {// offer user update
            Dialog = new AlertDialog.Builder(MainActivity.this).create();
            Dialog.setMessage("Update app to the latest version?");
            Dialog.setButton(AlertDialog.BUTTON_NEUTRAL, "update",
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int which) {
                            NSOMUC.downloadUpdates();
                        }
                    });
            Dialog.show();
...

问题是,这完全正常,它显示了对话框,但在每次第二次启动时,它都会因异常而崩溃。它在下次发布时工作正常。然后在下一次崩溃。

 android.view.WindowManager$BadTokenException: Unable to add window -- token android.os.BinderProxy@89b1ec1 is not valid; is your activity running?

这就是我发送广播的方式

   // finally send broadcast to MainActivity
        Intent intent = new Intent( "intentKey" );
        intent.putExtra( "status", status_string );
        LocalBroadcastManager.getInstance( context ).sendBroadcast( intent );

这真让我疯了。

0 个答案:

没有答案