首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用幻灯片时Android中的空指针异常

使用幻灯片时Android中的空指针异常
EN

Stack Overflow用户
提问于 2022-06-20 10:29:05
回答 3查看 294关注 0票数 -2

这是logcat输出

代码语言:javascript
复制
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中,当使用幻灯片加载图像时会出现异常。100号线.into(uImage)

代码语言:javascript
复制
 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)
    }

}
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2022-06-20 11:18:42

据我所知,您正在错误地初始化BottomSheetBottomSheets。如果我没有错,您正试图在一个BottomSheet中显示这个Activity,如果是,请尝试以下代码:

代码语言:javascript
复制
    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()
    }
票数 1
EN

Stack Overflow用户

发布于 2022-06-20 10:54:14

this in Glide.with(this)它代表什么?您应该在那里传递一个视图或上下文。

代码语言:javascript
复制
Glide.with(requireContext())  //for example
.load(ownerModel.profileImage)
.into(uImage)
票数 0
EN

Stack Overflow用户

发布于 2022-06-20 10:59:27

很难知道null的确切位置,因为我们没有您的项目,但是您有,所以一个断点会为您和其他人澄清这一点。

代码语言:javascript
复制
ownersRef.get().addOnSuccessListener { document ->
        val ownerModel = document.toObject(Owner::class.java)
        if (ownerModel != null) {
            Glide.with(this)
                .load(ownerModel.profileImage)
                .into(uImage)
        }

在此代码中,可以有各种空值。

ownerRef您在firebase上执行get(),但您确定这不是null吗?

this (在Glide.with(...)中)--这里可能指的是lambda中addOnSuccessListener的匿名实例,而不是活动或片段。

ownerModel不是空的,因为您检查了

uImage可能是空的,但我不知道Glide是否关心。

放置一个断点,调试应用程序,注意您有一个NullPointerReference异常。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72685672

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档