Kotlin 应用程序由于空指针异常而崩溃



我正在尝试在我的 Kotlin 应用中使用 Firebase 的移动身份验证。我的应用程序不断崩溃,显示空指针异常,我最好的猜测是"代码"变量保持空,即使我在有趣的验证完成(凭据:PhoneAuthCredential(中为其分配了一个值。

我的 Kotlin 代码:

package com.example.myhospital
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.view.View
import android.widget.Toast
import com.google.android.gms.tasks.Task
import com.google.firebase.FirebaseException
import com.google.firebase.FirebaseTooManyRequestsException
import com.google.firebase.auth.*
import kotlinx.android.synthetic.main.activity_otp_auth.*
import java.util.concurrent.TimeUnit
class otp_auth : AppCompatActivity() {
lateinit var mauth:FirebaseAuth
var storedVerificationId:String?=null
//    var resendToken:PhoneAuthProvider.ForceResendingToken?=null
var code:String?=null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_otp_auth)
mauth=FirebaseAuth.getInstance()
bu_send_otp.setOnClickListener {
view:View->verify()
}

}

private fun verify(){
val phnNo=enter_mobile.text.toString()
PhoneAuthProvider.getInstance().verifyPhoneNumber(
"+91"+phnNo, // Phone number to verify
60, // Timeout duration
TimeUnit.SECONDS, // Unit of timeout
this, // Activity (for callback binding)
mcallbacks) // OnVerificationStateChangedCallbacks
}

var mcallbacks = object : PhoneAuthProvider.OnVerificationStateChangedCallbacks() {
override fun onVerificationCompleted(credential: PhoneAuthCredential) {
// This callback will be invoked in two situations:
// 1 - Instant verification. In some cases the phone number can be instantly
//     verified without needing to send or enter a verification code.
// 2 - Auto-retrieval. On some devices Google Play services can automatically
//     detect the incoming verification SMS and perform verification without
//     user action.
buVerify.setOnClickListener { view: View ->
code = enter_OTP.text.toString()          //Assigned a value to code
}
signInWithPhoneAuthCredential(credential)
}
override fun onVerificationFailed(e: FirebaseException) {
// This callback is invoked in an invalid request for verification is made,
// for instance if the the phone number format is not valid.
if (e is FirebaseAuthInvalidCredentialsException) {
Toast.makeText(applicationContext,"Chal Be!!",Toast.LENGTH_SHORT).show()
}
}
override fun onCodeSent(
verificationId: String,
token: PhoneAuthProvider.ForceResendingToken
) {
// The SMS verification code has been sent to the provided phone number, we
// now need to ask the user to enter the code and then construct a credential
// by combining the code with a verification ID.
super.onCodeSent(verificationId, token)
// Save verification ID and resending token so we can use them later
storedVerificationId = verificationId
}
}

val credential = PhoneAuthProvider.getCredential(storedVerificationId!!, code!!)

private fun signInWithPhoneAuthCredential(credential: PhoneAuthCredential) {
mauth.signInWithCredential(credential).addOnCompleteListener{ task:Task<AuthResult> ->
if (task.isSuccessful) {
// Sign in success, update UI with the signed-in user's information
val intent = Intent(this,book_appoint::class.java)
startActivity(intent)
} else {
// Sign in failed, display a message and update the UI
if (task.exception is FirebaseAuthInvalidCredentialsException) {
Toast.makeText(applicationContext,"WRONG!!",Toast.LENGTH_SHORT).show()
}
}
}
}

}

我的日志猫在以下位置显示错误:

val credential = PhoneAuthProvider.getCredential(storedVerificationId!!, code!!)

原因:科特林。KotlinNullPointerException at com.example.myhospital.otp_auth.(otp_auth.kt:89(

我的日志猫:

2019-12-31 02:27:42.496 16222-16222/? E/mple.myhospita: Unknown bits set in runtime_flags: 0x8000
2019-12-31 02:27:45.804 16222-16222/com.example.myhospital E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.myhospital, PID: 16222
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.myhospital/com.example.myhospital.otp_auth}: kotlin.KotlinNullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3194)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3409)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2016)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7356)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
Caused by: kotlin.KotlinNullPointerException
at com.example.myhospital.otp_auth.<init>(otp_auth.kt:89)
at java.lang.Class.newInstance(Native Method)
at android.app.AppComponentFactory.instantiateActivity(AppComponentFactory.java:95)
at androidx.core.app.CoreComponentFactory.instantiateActivity(CoreComponentFactory.java:43)
at android.app.Instrumentation.newActivity(Instrumentation.java:1243)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3182)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3409) 
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83) 
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135) 
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2016) 
at android.os.Handler.dispatchMessage(Handler.java:107) 
at android.os.Looper.loop(Looper.java:214) 
at android.app.ActivityThread.main(ActivityThread.java:7356) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930) 

我的XML:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".otp_auth">
<EditText
android:id="@+id/enter_mobile"
android:layout_width="359dp"
android:layout_height="73dp"
android:layout_marginTop="56dp"
android:ems="10"
android:hint="Enter Your Mobile No."
android:inputType="phone"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/bu_send_otp"
android:layout_width="151dp"
android:layout_height="56dp"
android:layout_marginTop="28dp"
android:text="Send OTP"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/enter_mobile" />
<EditText
android:id="@+id/enter_OTP"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:hint="Enter OTP"
android:inputType="number"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="@+id/bu_send_otp"
app:layout_constraintHorizontal_bias="0.235"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/bu_send_otp"
app:layout_constraintVertical_bias="0.246" />
<Button
android:id="@+id/buVerify"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="68dp"
android:layout_marginBottom="256dp"
android:fontFamily="monospace"
android:text="Verify"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.384"
app:layout_constraintStart_toEndOf="@+id/enter_OTP" />
</androidx.constraintlayout.widget.ConstraintLayout>

我们应该知道 "?" 和 "!!" 运算符之间的基本区别。 "?" 将执行程序,甚至变量为 null 并且 "!!" 会产生 null 指针异常。

这是另一个运算符,即 Elvis 运算符,即使变量为 null,它也为空值提供有意义的完整值。

因此,根据要求,您可以尝试此代码

var storedVerificationId:String?=null
val storeverificationIdEvenNull: String= if (storedVerificationId:String != null) storedVerificationId:String.length else "-1"

并像更改代码一样

val credential = PhoneAuthProvider.getCredential(storedVerificationId, code)

现在,如果该值为 null,那么它将始终在存储的验证 ID 中分配"-1"作为字符串值,您可以根据 ID 执行操作。

最新更新