【问题标题】:Android Kotlin: Cannot load a firebase storage image into fragment using glideAndroid Kotlin:无法使用 glide 将 firebase 存储图像加载到片段中
【发布时间】:2020-12-24 22:22:53
【问题描述】:

我试图在打开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 都不为空

【问题讨论】:

  • 很遗憾,imv_my_profile_picture 为空。
  • 将您的代码切换到 onViewCreated() 方法。

标签: android firebase kotlin firebase-storage android-glide


【解决方案1】:

这是因为视图本身还没有初始化。

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

【讨论】:

    猜你喜欢
    • 2020-06-28
    • 1970-01-01
    • 2021-05-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-18
    • 2017-01-12
    • 1970-01-01
    相关资源
    最近更新 更多