- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有两个类(class)
BroadcastReceiver
并从新到达的 SMS 中提取一次性密码(以后称为 OTP)的类。回调有一个方法onOTPReceived(long timeReceived, final String otp)
由OTPBroadcastReceiver 执行并在消息以long
和OTP 消息作为 String
。
我的 OTPListener 中有一个内部类,它具有以下成员变量:-
private class OTPModel {
long timeReceived = 0L;
String otp = null;
}
此类用于存储 OTP 消息和接收消息的时间。
OTPListener
代码如下import android.content.Context;
import android.support.annotation.NonNull;
import android.widget.Toast;
import java.util.List;
class OTPListener {
private OTPReceivedInternal mOTPReceivedCallback;
private OTPModel mOTPModel;
OTPListener(@NonNull final Context context,
final List<SMSRule> smsRules,
@NonNull final CustomOTPView.OnOTPReceivedCallback onOTPReceivedCallback) {
mOTPModel = new OTPModel();
mOTPReceivedCallback = new OTPReceivedInternal() {
@Override
public void onOTPReceived(long timeReceived, String otp) {
if (mOTPModel.timeReceived == 0L) {
mOTPModel.timeReceived = timeReceived;
mOTPModel.otp = otp;
onOTPReceivedCallback.onOTPReceived(mOTPModel.otp);
System.out.println("First execution");
System.out.println("Value of mOTPModel.otp:" + mOTPModel.otp);
System.out.println(String.valueOf(mOTPModel.timeReceived));
} else {
if (!mOTPModel.otp.equals(otp)) {
if (timeReceived > mOTPModel.timeReceived) {
Toast.makeText(context, String.valueOf(timeReceived > mOTPModel.timeReceived),Toast.LENGTH_SHORT).show();
mOTPModel.timeReceived = timeReceived;
onOTPReceivedCallback.onOTPReceived(mOTPModel.otp = otp);
System.out.println("After first execution");
System.out.println("{{Value of mOTPModel.timeReceived:" + String.valueOf(mOTPModel.timeReceived));
System.out.println("Value of mOTPModel.otp:" + String.valueOf(mOTPModel.otp)+"}}");
}
}
}
}
};
new OTPBroadcastReceiver(context,smsRules,mOTPReceivedCallback);
}
private class OTPModel {
long timeReceived = 0L;
String otp = null;
}
protected interface OTPReceivedInternal {
void onOTPReceived(long timeReceived, String otp);
}
这里的问题是,
第一次它执行,
mOTPModel.timeReceived
和 mOTPModel.otp
即使在下面的赋值语句之后都是空的
mOTPModel.timeReceived = timeReceived;
mOTPModel.otp = otp;
唯一在我的 Android 显示器上打印的是
System.out.println("First execution");
紧随其后的两个语句甚至都不会执行。
第二次它执行时,代码的else
部分被执行,但这次它打印mOTPModel.otp
的旧值> 和 mOTPModel.timeReceived
以及后续执行继续显示之前的值。
这里出了什么问题?为什么没有正确分配 OTPModel 类的成员变量?
感谢您的宝贵时间!
最佳答案
我相信您的问题的原因不在您显示的代码范围内。监听器在哪里实例化?是否创建了它的多个实例?
您如何确定 otp
和 timeReceived
在赋值后为空?也许也可以检查 otp
和 timeReceived
的传入值(通过调试或使用更多日志记录语句)。
未看到某些 System.out.println
的事实一定是您的日志记录有问题,或者是某些停止进程的调试/线程问题。
进一步评论:
private OTPReceivedInternal mOTPReceivedCallback;
混淆字段名称,我将其重命名为 otpReceivedInternal
。特别是因为还有一个名为 onOTPReceivedCallback
的构造函数参数。
onOTPReceivedCallback.onOTPReceived(mOTPModel.otp = otp);
令人困惑的成语。我会把它分成:
mOTPModel.otp = otp;
onOTPReceivedCallback.onOTPReceived(otp);
这更具可读性。
编辑:这就是我现在建议的更改,以了解正在发生的事情
@Override
public void onOTPReceived(long timeReceived, String otp) {
// print the received values
System.out.printf("timeReceived = %s, otp = %s", timeReceived, otp);
if (mOTPModel.timeReceived == 0L) {
mOTPModel.timeReceived = timeReceived;
mOTPModel.otp = otp;
onOTPReceivedCallback.onOTPReceived(mOTPModel.otp);
// print the mOTPModel values
System.out.printf("1: mOTPModel.timeReceived = %s, mOTPModel.otp= %s", mOTPModel.timeReceived, mOTPModel.otp);
System.out.flush();
// ... carry on
关于java - 内部类成员变量未正确分配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39466454/
我是一名优秀的程序员,十分优秀!