我最近偶然發現了一個運行時錯誤,無論出于何種原因,它都會使我的應用程式崩潰,而且我無法深入了解它。基本上,我通過將其存盤在哈希圖中來更新用戶組態檔資料。這是用戶類的樣子:
@Parcelize
data class User(
val id: String = "",
val firstName: String = "",
val lastName: String = "",
val email: String = "",
val image: String = "",
val mobile: Long = 0,
val gender: String = "",
val profileCompleted: Int = 0): Parcelable
在我稱為 UserProfileActivity 的活動中,我將手機和性別存盤到一個哈希圖中,然后呼叫 Firebase 函式來更新 Firestore 資料庫。這是活動的方法。當單擊“提交”按鈕時,此代碼運行:
btn_submit.setOnClickListener {
if(validateUserProfileDetails()){ //checks if entered credentials are valid
val userHashMap = HashMap<String, Any>() //creates the hashmap
val mobileNumber = et_mobile_number.text.toString().trim { it <= ' ' }
val gender = if (rb_male.isChecked) { //these are radio buttons, only 1 clicked
Constants.MALE
} else {
Constants.FEMALE
}
userHashMap[Constants.MOBILE] = mobileNumber.toLong() //storing info in hashmap
userHashMap[Constants.GENDER] = gender
showProgressDialog(resources.getString(R.string.please_wait)) //starting a progress dialog
FirestoreClass().updateUserProfileData( //method in FirestoreClass
this, userHashMap
)
}
}
現在與資料庫通信的被呼叫方法:
fun updateUserProfileData(activity: Activity, userHashMap: HashMap<String, Any>) {
mFireStore.collection(Constants.USERS) // collection named "users"
.document(getCurrentUserID()) //getCurrentUserID() just gets the current user id
.update(userHashMap) // hashmap used to update the user
.addOnSuccessListener {
when (activity) {
is UserProfileActivity -> {
activity.userProfileUpdateSuccess() //this just hides the progress dialog and finishes the activity in the UserProfileActivity
}
}
}
.addOnFailureListener { e ->
when (activity) {
is UserProfileActivity -> {
activity.hideProgressDialog()
}
}
}
}
但我收到此錯誤:
2021-12-27 02:35:38.727 14960-14960/com.example.myshopapp E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.myshopapp, PID: 14960
java.lang.NullPointerException: Parameter specified as non-null is null: method kotlin.jvm.internal.Intrinsics.checkNotNullParameter, parameter it
at com.example.myshopapp.firestore.FirestoreClass.updateUserProfileData$lambda-2(Unknown Source:7)
at com.example.myshopapp.firestore.FirestoreClass.$r8$lambda$vs4EuaGwStcL-i3lXRUecduDHWM(Unknown Source:0)
at com.example.myshopapp.firestore.FirestoreClass$$ExternalSyntheticLambda4.onSuccess(Unknown Source:4)
at com.google.android.gms.tasks.zzm.run(com.google.android.gms:play-services-tasks@@18.0.0:1)
at android.os.Handler.handleCallback(Handler.java:938)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:223)
at android.app.ActivityThread.main(ActivityThread.java:7656)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
我完全不知道空指標例外在哪里發生......而且很有趣(這很重要),資料庫確實得到了正確更新,為什么它會崩潰?任何幫助將非常感激。:)
uj5u.com熱心網友回復:
這個特定錯誤最近經常出現,似乎是由于 Firebase 偵聽器中不一致的可空性注釋。
顯然谷歌已經意識到這個問題并且最近可能已經修復了它
在 Kotlin 中添加onSuccessListener
forupdate
呼叫時,它會為it
( Void
not Void?
)推斷出非空型別。這意味著如果 Firestore 回傳空結果(這對于Void
來自 java的引數來說并不少見),它會引發您在將其轉換為非空引數時顯示的錯誤。解決方案是更新到修復此問題的最新版本,或者如果您需要立即解決方法來專門將型別設定為可空,以便更改
.addOnSuccessListener {
println("Updated doc")
}
到
.addOnSuccessListener { _: Void? ->
println("Updated doc")
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/395206.html
標籤:安卓 火力基地 科特林 谷歌云firestore 空指针异常