簡體   English   中英

由於空指針異常導致 Kotlin 應用程序崩潰

[英]Kotlin App Crashing due to NULL POINTER EXCEPTION

我正在嘗試在我的 Kotlin 應用程序中使用 Firebase 的移動身份驗證。 我的應用程序不斷崩潰,顯示空指針異常,我最好的猜測是“代碼”變量仍然為空,即使我在 fun onVerificationCompleted(credential: PhoneAuthCredential) 中為其分配了一個值。

我的科特林代碼:

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()
                }
            }
        }
    }


}

我的 logcat 在以下位置顯示錯誤:

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

引起:com.example.myhospital.otp_auth.(otp_auth.kt:89) 的 kotlin.KotlinNullPointerException

我的日志:

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>

我們應該知道“?”之間的基本區別。 和 ”!!” 操作員。 “?” 將執行程序,即使變量為空和“!!” 產生空指針異常。

這里還有一個運算符,即 Elvis 運算符,即使變量為空,它也會為空值提供有意義的完整值。

因此,根據要求,您可以嘗試此代碼

var storedVerificationId:String?=null

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

並更改代碼,如

val credential = PhoneAuthProvider.getCredential(storedVerificationId, code)

現在,如果該值為空,那么它將始終將“-1”分配為 storedVerificationId 中的字符串值,您可以根據 ID 執行操作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM