gpt4 book ai didi

java - 内部类成员变量未正确分配

转载 作者:行者123 更新时间:2023-11-30 01:00:43 24 4
gpt4 key购买 nike

我有两个类(class)

  1. OTPBroadcastReceiver - 一个扩展 BroadcastReceiver 并从新到达的 SMS 中提取一次性密码(以后称为 OTP)的类。
  2. OTPListener - 一个实际启动上一个类的类,其中包含一个回调,应该在收到 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.timeReceivedmOTPModel.otp

即使在下面的赋值语句之后都是空的

mOTPModel.timeReceived = timeReceived;
mOTPModel.otp = otp;

唯一在我的 Android 显示器上打印的是

System.out.println("First execution");

紧随其后的两个语句甚至都不会执行。

第二次它执行时,代码的else部分被执行,但这次它打印mOTPModel.otp的旧值> 和 mOTPModel.timeReceived 以及后续执行继续显示之前的值。

这里出了什么问题?为什么没有正确分配 OTPModel 类的成员变量?

感谢您的宝贵时间!

最佳答案

我相信您的问题的原因不在您显示的代码范围内。监听器在哪里实例化?是否创建了它的多个实例?

您如何确定 otptimeReceived 在赋值后为空?也许也可以检查 otptimeReceived 的传入值(通过调试或使用更多日志记录语句)。

未看到某些 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com