android - 无法添加窗口—— token android.os.BinderProxy@42824 无效;你的 Activity 在运行吗?

标签 android dialog android-pendingintent

我有一个带有 pendingIntent 的短信发送器,一旦发送短信,它就会打开一个对话框。 这是来自 SMS 发件人的代码

public final void sendSmsByManager(final String code) 
{
        progressDialog = new ProgressDialog(Registration.this);
        progressDialog.setTitle("Please wait");
        progressDialog.setMessage("Sending SMS Verification Code...");
        progressDialog.setCancelable(false);
        progressDialog.setCanceledOnTouchOutside(false);
        progressDialog.show();

    String sent = "SMS_SENT";

    PendingIntent sentPI = PendingIntent.getBroadcast(Registration.this,

    2, new Intent(sent), 0);

    Registration.this.registerReceiver(new BroadcastReceiver() {
        @Override
        public void onReceive(Context arg0, Intent arg1) {

            switch (getResultCode())

            {

            case Activity.RESULT_OK:

                // Toast.makeText(getBaseContext(), "SMS sent",

                // Toast.LENGTH_SHORT).show();
                progressDialog.dismiss();
                dialog_confirm(code,Registration.this);
                // saveRegistration();

                break;

            case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
                progressDialog.dismiss();

                Toast.makeText(getApplicationContext(), "Generic failure",

                Toast.LENGTH_SHORT).show();

                // Toast.makeText(getApplicationContext(), "code" + code,

                // Toast.LENGTH_SHORT).show();

                status = "Generic failure";

                break;

            case SmsManager.RESULT_ERROR_NO_SERVICE:
                progressDialog.dismiss();
                Toast.makeText(
                        myContext,
                        "No service. Check mobile network connection." + ""
                                + "",

                        Toast.LENGTH_SHORT).show();

                status = "No service";

                break;

            case SmsManager.RESULT_ERROR_NULL_PDU:
                progressDialog.dismiss();
                Toast.makeText(getApplicationContext(), "Null PDU",

                Toast.LENGTH_SHORT).show();

                status = "Null PDU";

                break;

            case SmsManager.RESULT_ERROR_RADIO_OFF:
                progressDialog.dismiss();
                Toast.makeText(getApplicationContext(), "Radio off",

                Toast.LENGTH_SHORT).show();

                status = "Radio off";

                break;

            }

        }

    }, new IntentFilter(sent));

    SmsManager sms = SmsManager.getDefault();

    sms.sendTextMessage(txt_mobileno.getText().toString(), null, code,
            sentPI, null);

}

然后我从 Activity.RESULT_OK 调用对话框

 public void dialog_confirm(final String SMSCode,Context context) 
 {

     dialog = new Dialog(context);


    dialog.getWindow().requestFeature(Window.FEATURE_NO_TITLE);
    dialog.getWindow().setBackgroundDrawable(
            new ColorDrawable(android.graphics.Color.TRANSPARENT));
    dialog.setContentView(R.layout.dialog_confirm);

    final EditText txt_code = (EditText) dialog.findViewById(R.id.txt_code);
    Button btn_add = (Button) dialog.findViewById(R.id.btn_confirm);
    Button btn_cancel = (Button) dialog.findViewById(R.id.btn_cancel);

    btn_add.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            String code = txt_code.getText().toString();

            if (code.equals(SMSCode)) {
                match = true;
                // save
                if (Constants.ID != 0) {

                    updateRegistration();

                } else {

                    saveRegistration();

                }

                dialog.dismiss();

            } else {
                DialogUtil.createErrorDialog(Registration.this, "Registration Error",
                        "SMS verification code does not match!").show();

            }

        }

    });

    btn_cancel.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            dialog.dismiss();
        }
    });

    dialog.setCancelable(false);
    dialog.show();
}

dialog.show 上的错误点 这是屏幕截图。 enter image description here

最佳答案

问题可能是上下文在本应显示对话框时被破坏了。这可以这样避免:

if(!((Activity) context).isFinishing())
{
    //show dialog
}

关于android - 无法添加窗口—— token android.os.BinderProxy@42824 无效;你的 Activity 在运行吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32352124/

相关文章:

javascript - 如何在 React 中使用多个 Material 的 ui 对话框?

php - 从 PHP 使用 linux 'dialog' 命令

android - 如何避免重复 PendingIntents

android - 小部件发送多种类型的 Intent

android - 在 XML 文件中指定 var 类型的原因是什么?

android - 如何在不使用第三方库的情况下开发 PDF 查看器

android - 我如何从我的可绘制对象中找到在 imageview 中设置的图像?

java - 安卓 : How to synchronized resource in right way

android - APP后台运行时如何将对话框显示在最上面?

android - getParcelableExtra 在 Pending Intent 上总是 null,但不是 Intent