我正在尝试从自动接收的短信中获取OTP(一次性密码)。我在3个项目中使用了这个代码,在打印变量之前和之后我从未遇到过这个错误,然后我得到了一个值。那么msgBody如何为空对象抛出错误
if (bundle != null) {
//---retrieve the SMS message received---
try {
Object[] pdus = (Object[]) bundle.get("pdus");
msgs = new SmsMessage[pdus.length];
for (int i = 0; i < msgs.length; i++) {
msgs[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);
msg_from = msgs[i].getOriginatingAddress();
msgBody = msgs[i].getMessageBody();
}
if (OTPFragment.onReciveSMS != null) {
msgBody = msgBody.replaceAll("[^\\d.]", "");
msgBody = msgBody.replace(".", "");
OTPFragment.onReciveSMS.setOnReciveSMS("sms", msgBody.trim());
}
} catch (Exception e) {
Log.d("SMS Listener Exception",e.toString());
}