Ian
Ian

Reputation: 334

Android Kotlin: Cannot load a firebase storage image into fragment using glide

I am trying to show the user their profile picture when opening MyProfileEdit fragment.

I am currently getting a null pointer exception

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)

when I try to get the user image from firebase storage:

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

I checked the log but both currentUser and photoUrl are not null

Upvotes: 0

Views: 160

Answers (1)

vikas kumar
vikas kumar

Reputation: 11018

It's because the view is itself not initialized yet.

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

you should do glide image loading in onViewCreated()

for example

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

Upvotes: 1

Related Questions