簡體   English   中英

使用 Glide 時 Android 中的空指針異常

[英]Null pointer Exception in Android when using Glide

這是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)
    }

}

據我了解,您正在初始化ViewBottomSheet錯誤。 如果我沒記錯,您正嘗試在Activity中顯示此BottomSheet ,如果是,請嘗試以下代碼:

    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
            }
        }
        
        // Add this line to show the BottomSheet
        bottomSheetDialog.show()
    }

Glide.with(this) this的 this 代表什么? 您應該在那里傳遞視圖或上下文

Glide.with(requireContext())  //for example
.load(ownerModel.profileImage)
.into(uImage)

很難說 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上,但你確定那不是空的嗎?

Glide.with(...)中的this可能是指 lambda 中addOnSuccessListener的匿名實例,而不是 Activity 或 Fragment。

ownerModel不為空,因為您檢查

uImage可以為空,但我不知道 Glide 是否關心。

放置一個斷點,調試您的應用程序,並注意到您有一個 NullPointerReference 異常。

暫無
暫無

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

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