我正在执行某些后台操作时显示进度对话框。我正在使用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!
当然,进度对话框在仿真器中被取消,但在真实设备上却没有。
我完全失去了。关于如何调试此问题的任何建议都非常感谢。