0

在执行某些后台操作时,我正在显示一个进度对话框。我正在使用 Eventbus 发回显示对话框的片段。我正在使用一个变量来保存对对话框的引用。当我使用 Genymotion 模拟器测试这个程序时,它运行良好。当我在真实设备上测试它时,变量变为空。

public ProgressDialog mAuthorizeProgress;

首先,用户会看到一个对话框,他们在其中输入一个 4 位代码。当他们单击“确定”时,将处理 4 位代码(异步)并显示 ProgressDialog(mAuthorizeProgress)。

用户单击确定后,将显示进度对话框:

protected void sendProtectedCommand(OmniCommand cmd) {
    mPendingCommand = cmd;
    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(mContext);
    if (prefs.getBoolean("pref_key_model_code_required",false)) {
        AlertDialog.Builder alert = new AlertDialog.Builder(mContext);
        alert.setTitle("Enter Code");
        final EditText input = new EditText(mContext);
        input.setInputType(InputType.TYPE_CLASS_NUMBER);
        input.setRawInputType(Configuration.KEYBOARD_12KEY);
        alert.setView(input);
        alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int whichButton) {
                //Put actions for OK button here
                int code = 0;
                try {
                    code = Integer.parseInt(input.getText().toString());
                } catch (Exception e) {
                    Log.e(tag, "Exception="+e.toString());
                }
                sendRequest(Omni.REQ_SECURITY_VALIDATION, OmniRequest.securityValidation(code));
                showAuthorizeProgress();
                if (mAuthorizeProgress !=null) {
                    Log.d(tag, "mAuthorizeProgress is NOT null right after being shown.");
                } else {
                    Log.d(tag, "mAuthorizeProgress is null right after being shown.");
                }
                mHandler.postDelayed(() -> {
                    Log.d(tag, "authorization timeout is over.");
                    hideAuthorizeProgress("Authorization Timed Out");
                }, AUTHORIZATION_PROGRESS_TIMEOUT);
            }
        });
        alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int whichButton) {
                //Put actions for CANCEL button here, or leave in blank
                render(); // should reset spinner
            }
        });
        AlertDialog alertToShow = alert.create();
        alertToShow.getWindow().setSoftInputMode(
                WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
        alertToShow.show();
    } else {
        sendCommand(cmd);
    }
}

这是显示进度对话框的函数:

public void showAuthorizeProgress() {
    Log.d(tag, "showAuthorizeProgress()");
    mPendingCodeResponse = true;
    mAuthorizeProgress = ProgressDialog.show(
            mContext,
            "Security Validation",
            "Validating Provided User Code",
            true, false);
    if (mAuthorizeProgress !=null) {
        Log.d(tag, "mAuthorizeProgress is NOT null right after creation.");
    } else {
        Log.d(tag, "mAuthorizeProgress is null right after creation.");
    }
    if (mPendingCodeResponse) {
        Log.d(tag, "mPendingCodeResponse is TRUE right after creation.");
    } else {
        Log.d(tag, "mPendingCodeResponse is FALSE right after creation.");
    }
}

这是解除代码的片段:

public void onEventMainThread(E.StorageChanged e) {
            ...
            if (mAuthorizeProgress !=null) {
                Log.d(tag, "mAuthorizeProgress is NOT null right before being hidden.(s)");
            } else {
                Log.d(tag, "mAuthorizeProgress is null right before being hidden.(s)");
            }
            String msg = "Authorization Success!";
            Log.d(tag, "hideAuthorizeProgress(): msg="+msg);
            mPendingCodeResponse = false;
            if (mAuthorizeProgress !=null) {
                mAuthorizeProgress.dismiss();
            } else {
                Log.d(tag, "mAuthorizeProgress is null.");
            }
            ...

这是来自真实设备的日志:

12-18 11:30:11.034 25448 25448 D GenericCard: mAuthorizeProgress is null right before being hidden.(s)
12-18 11:30:11.034 25448 25448 D GenericCard: hideAuthorizeProgress(): msg=Authorization Success!
12-18 11:30:11.034 25448 25448 D GenericCard: mAuthorizeProgress is null.

这是来自模拟器的日志:

12-18 12:38:09.914 12560-12560/com.app D/GenericCard: mAuthorizeProgress is NOT null right before being hidden.(s)
12-18 12:38:09.914 12560-12560/com.app D/GenericCard: hideAuthorizeProgress(): msg=Authorization Success!

当然,进度对话框在模拟器中被关闭,但在真实设备上却没有。

我完全不知所措。非常感谢有关如何调试此问题的任何建议。

4

0 回答 0