我正在使用Firebase电话身份验证,并在广播接收器的帮助下将顶部提取到应用程序中。下面我分享了OTPAutoReader代码,我正面临这个问题-
Caused by java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.Class.isInterface()' on a null object reference
at java.lang.Class.isAssignableFrom(Class.java:589)
at android.os.Parcel.readParcelableCreatorInternal(Parcel.java:4882)
at android.os.Parcel.readParcelableInternal(Parcel.java:4795)
at android.os.Parcel.readValue(Parcel.java:4561)
at android.os.Parcel.readValue(Parcel.java:4341)
at android.os.Parcel.-$$Nest$mreadValue()
at android.os.Parcel$LazyValue.apply(Parcel.java:4439)
at android.os.Parcel$LazyValue.apply(Parcel.java:4398)
at android.os.BaseBundle.getValueAt(BaseBundle.java:394)
at android.os.BaseBundle.getValue(BaseBundle.java:374)
at android.os.BaseBundle.getValue(BaseBundle.java:357)
at android.os.BaseBundle.get(BaseBundle.java:696)
at android.os.Bundle.getParcelable(Bundle.java:947)
at com.adithya.memoneet.v1.presentation.auth.OTPAutoReader.onReceive(OTPAutoReader.kt:27)
at android.app.ActivityThread.handleReceiver(ActivityThread.java:4544)
at android.app.ActivityThread.-$$Nest$mhandleReceiver()
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2308)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loopOnce(Looper.java:240)
at android.os.Looper.loop(Looper.java:351)
at android.app.ActivityThread.main(ActivityThread.java:8381)
at java.lang.reflect.Method.invoke(Method.java)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:584)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1013)
以上问题是由本线引起的-
bundle.getParcelable(SmsRetriever.EXTRA_STATUS, Status::class.java)
下面的代码-
class OTPAutoReader : BroadcastReceiver() {
private var otpSmsListener: OTPSMSListener? = null
fun init(otpSmsListener: OTPSMSListener) {
this.otpSmsListener = otpSmsListener
}
override fun onReceive(context: Context?, intent: Intent?) {
if (SmsRetriever.SMS_RETRIEVED_ACTION == intent?.action) {
val bundle: Bundle? = intent.extras
if (bundle != null) {
val status: Status? = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
bundle.getParcelable(SmsRetriever.EXTRA_STATUS, Status::class.java)
} else {
bundle.getParcelable(SmsRetriever.EXTRA_STATUS)
}
if (status != null) {
when (status.statusCode) {
CommonStatusCodes.SUCCESS -> {
val message: String? = bundle.getString(SmsRetriever.EXTRA_SMS_MESSAGE)
Log.i("TAG >> SUCCESS", message ?: "")
message?.let {
try {
val pattern = Pattern.compile("\\d{6}")
val matcher = pattern.matcher(message)
if (matcher.find()) {
val otp = matcher.group(0)
if (otp != null) {
otpSmsListener?.onSuccess(otp)
} else {
otpSmsListener?.onFailure("OTP not receive")
}
} else {
otpSmsListener?.onFailure("OTP not receive")
}
} catch (e: Exception) {
e.printStackTrace()
otpSmsListener?.onFailure("Failed to retrieve SMS")
}
} ?: run {
otpSmsListener?.onFailure("Failed to retrieve SMS")
}
}
CommonStatusCodes.TIMEOUT -> {
Log.i("TAG >> TIMEOUT", "timeout")
otpSmsListener?.onFailure("timeout")
}
}
}
}
}
}
interface OTPSMSListener {
fun onSuccess(otp: String)
fun onFailure(msg: String)
}
}
注意:此问题仅在Android 13上出现。
1条答案
按热度按时间u4vypkhs1#
这看起来类似于this question,其中的解决方案是使用来自
androidx.core:core-ktx:1.10.0
的BundleCompat.getParcelable(Bundle, String, Class)
而不是getParcelable(String, Class)
。