這是logcat的輸出
2022-06-20 13:19:27.605 20830-20830/com.example.moveapplication E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.moveapplication, PID: 20830
java.lang.NullPointerException: Argument must not be null
at com.bumptech.glide.util.Preconditions.checkNotNull(Preconditions.java:29)
at com.bumptech.glide.util.Preconditions.checkNotNull(Preconditions.java:23)
at com.bumptech.glide.RequestBuilder.into(RequestBuilder.java:768)
at com.example.moveapplication.activities.MainActivity.openBottomSheetDialog$lambda-2(MainActivity.kt:100)
at com.example.moveapplication.activities.MainActivity.$r8$lambda$acEx44X-0qqDdGYpms4KzfGag5A(Unknown Source:0)
at com.example.moveapplication.activities.MainActivity$$ExternalSyntheticLambda3.onSuccess(Unknown Source:15)
at com.google.android.gms.tasks.zzm.run(com.google.android.gms:play-services-tasks@@18.0.1:1)
at android.os.Handler.handleCallback(Handler.java:883)
at android.os.Handler.dispatchMessage(Handler.java:100)
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)
從我的 logcat 中,使用 Glide 加載影像時會發生例外。
第 100 行是.into(uImage)
private fun openBottomSheetDialog(position: Int) {
val uImage = findViewById<ImageView>(R.id.sheetIvProfileImage)
val uNumber = findViewById<TextView>(R.id.sheetTvOwnerNumber)
val uEmail = findViewById<TextView>(R.id.tvOwnerEmail)
val uAbout = findViewById<TextView>(R.id.tvOwnerAbout)
val uTitle = findViewById<TextView>(R.id.sheetTvOwnerTitle)
val publisher = posts[position].publisher
val ownersRef = FirebaseFirestore.getInstance().collection("owners").document(publisher)
ownersRef.get().addOnSuccessListener { document ->
val ownerModel = document.toObject(Owner::class.java)
if (ownerModel != null) {
Glide.with(this)
.load(ownerModel.profileImage)
.into(uImage)
}
uTitle.text = ownerModel?.username
uNumber.text = ownerModel?.phoneNumber
uEmail.text = ownerModel?.email
uAbout.text = ownerModel?.about
}
val bottomSheetDialog = BottomSheetDialog(this, R.style.BottomSheetDialogTheme)
val bottomSheetView = LayoutInflater.from(applicationContext).inflate(
R.layout.bottom_sheet_dialog,
findViewById(R.id.bottomSheet)
)
bottomSheetDialog.apply {
setContentView(bottomSheetView)
setCancelable(true)
}
}
uj5u.com熱心網友回復:
this它Glide.with(this)代表什么?您應該在那里傳遞視圖或背景關系
Glide.with(requireContext()) //for example
.load(ownerModel.profileImage)
.into(uImage)
uj5u.com熱心網友回復:
很難說 null 到底在哪里,因為我們沒有你的專案,但你有,所以斷點會為你和其他人澄清這一點。
ownersRef.get().addOnSuccessListener { document ->
val ownerModel = document.toObject(Owner::class.java)
if (ownerModel != null) {
Glide.with(this)
.load(ownerModel.profileImage)
.into(uImage)
}
在這段代碼中,可以有各種空值。
ownerRef你get()在firebase上做,但你確定那不是空的嗎?
thisGlide.with(...)這里可能指的是 lambda 中的匿名實體,addOnSuccessListener而不是 Activity 或 Fragment。
ownerModel不為空,因為您檢查
uImage可以為空,但我不知道 Glide 是否在乎。
放置一個斷點,除錯您的應用程式,并注意到您有一個 NullPointerReference 例外。
uj5u.com熱心網友回復:
據我了解,您正在初始化錯誤的Views BottomSheet。如果我沒記錯的話,您正嘗試在 中顯示此BottomSheet內容Activity,如果是,請嘗試以下代碼:
private fun openBottomSheetDialog(position: Int) {
// Initialize the View of the BottomSheet
val bottomSheetView = LayoutInflater.from(this).inflate(R.layout.bottom_sheet_dialog, findViewById(R.id.bottomSheet), false)
// Initialize the BottomSheetDialog
val bottomSheetDialog = BottomSheetDialog(this, R.style.BottomSheetDialogTheme).apply {
setCancelable(true)
// Set the View
setContentView(bottomSheetView)
}
/*
* To initialize the Views of BottomSheet,
* use parent view of the BottomSheet, i.e., bottomSheetView
* */
val uImage = bottomSheetView.findViewById<ImageView>(R.id.sheetIvProfileImage)
val uNumber = bottomSheetView.findViewById<TextView>(R.id.sheetTvOwnerNumber)
val uEmail = bottomSheetView.findViewById<TextView>(R.id.tvOwnerEmail)
val uAbout = bottomSheetView.findViewById<TextView>(R.id.tvOwnerAbout)
val uTitle = bottomSheetView.findViewById<TextView>(R.id.sheetTvOwnerTitle)
val publisher = posts[position].publisher
val ownersRef = FirebaseFirestore.getInstance().collection("owners").document(publisher)
ownersRef.get().addOnSuccessListener { document ->
val ownerModel = document.toObject(Owner::class.java)
if (ownerModel != null) {
Glide.with(this)
.load(ownerModel.profileImage)
.into(uImage)
// These Views should be inside
uTitle.text = ownerModel?.username
uNumber.text = ownerModel?.phoneNumber
uEmail.text = ownerModel?.email
uAbout.text = ownerModel?.about
}
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/493352.html
上一篇:Unity-對嵌套串列進行排序
