Android Kotlin:无法使用滑行将Firebase存储映像加载到片段中

问题描述

打开MyProfileEdit片段时,我试图向用户显示其个人资料图片

我当前收到空指针异常

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:669)
        at com.example.byfimvp.ui.MyProfileEditFragment.onCreateView(MyProfileEditFragment.kt:36)

当我尝试从Firebase存储中获取用户映像时:

    override fun onCreateView(
        inflater: LayoutInflater,container: ViewGroup?,savedInstanceState: Bundle?
    ): View? {
        // Inflate the layout for this fragment

        val currentUser = FirebaseAuth.getInstance().currentUser
        if (currentUser != null) {
            if (currentUser!!.photoUrl != null) {
                Log.d("MyProfileEditFragment","PHOTO URL: ${currentUser!!.photoUrl}")
                Glide.with(this).load(currentUser.photoUrl).into(imv_my_profile_picture)
            }
        }

        return inflater.inflate(R.layout.fragment_my_profile_edit,container,false)
    }

我检查了日志,但是currentUserphotoUrl都不为空

解决方法

这是因为视图本身尚未初始化。

   override fun onCreateView(
    inflater: LayoutInflater,container: ViewGroup?,savedInstanceState: Bundle?
): View? {
    // Inflate the layout for this fragment

   
    //view is yet to be initialixed
    return inflater.inflate(R.layout.fragment_my_profile_edit,container,false)
}

您应该在 onViewCreated()

中进行滑动图像加载

例如

 override fun onViewCreated(view: View,savedInstanceState: Bundle?) {
    super.onViewCreated(view,savedInstanceState)
     val currentUser = FirebaseAuth.getInstance().currentUser
     if (currentUser != null) {
        if (currentUser!!.photoUrl != null) {
            Log.d("MyProfileEditFragment","PHOTO URL: ${currentUser!!.photoUrl}")
            Glide.with(this).load(currentUser.photoUrl).into(imv_my_profile_picture)
        }
    }
}