无法添加窗口 - 令牌android.os.BinderProxy@42824无效;你的活动在运行吗?

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了无法添加窗口 - 令牌android.os.BinderProxy@42824无效;你的活动在运行吗?相关的知识,希望对你有一定的参考价值。

我有一个带有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
}
另一答案

如果在定义活动时在AndroidManifest.xml中添加了以下行

android:noHistory="true"

您在AndroidManifest.xml文件中定义的活动应如下所示

<activity
    android:name=".Signup"
    android:label="@string/title_activity_signup"
    android:parentActivityName=".LoginSignup"
    android:screenOrientation="portrait"
    android:theme="@style/AppTheme.NoActionBar">
    <meta-data
        android:name="android.support.PARENT_ACTIVITY"
        android:value="com.yourpackage.LoginSignup" />

以上是关于无法添加窗口 - 令牌android.os.BinderProxy@42824无效;你的活动在运行吗?的主要内容,如果未能解决你的问题,请参考以下文章

对话框抛出“无法添加窗口 - 令牌 null 不适用于应用程序”,getApplication() 作为上下文

Android 1.6:“android.view.WindowManager$BadTokenException:无法添加窗口——令牌 null 不适用于应用程序”

无法添加窗口 - 令牌android.os.BinderProxy@42824无效;你的活动在运行吗?

错误的窗口令牌,您无法在创建 Activity 之前或隐藏之后显示对话框

错误的窗口令牌,您无法在创建 Activity 之前或在广播接收器中隐藏异常之后显示对话框

令牌桶与固定窗口(流量突发)