Home > OS >  Picasso crashes when i scroll down on my recycler view
Picasso crashes when i scroll down on my recycler view

Time:11-01

When I scroll down on my recycler view it crashes. I am getting the data via retrofit. It does not do it when i comment the Picasso lines out. It also did not do it when i used a generic image.

2021-10-31 17:24:06.620 12778-12778/com.example.harrypottercaracters E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.harrypottercaracters, PID: 12778
java.lang.IllegalArgumentException: Path must not be empty.
    at com.squareup.picasso.Picasso.load(Picasso.java:332)
    at com.example.harrypottercaracters.adapters.RVAdapter.onBindViewHolder(RVAdapter.kt:33)
    at com.example.harrypottercaracters.adapters.RVAdapter.onBindViewHolder(RVAdapter.kt:14)
    at androidx.recyclerview.widget.RecyclerView$Adapter.onBindViewHolder(RecyclerView.java:7065)
    at androidx.recyclerview.widget.RecyclerView$Adapter.bindViewHolder(RecyclerView.java:7107)
    at androidx.recyclerview.widget.RecyclerView$Recycler.tryBindViewHolderByDeadline(RecyclerView.java:6012)
    at androidx.recyclerview.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:6279)
    at androidx.recyclerview.widget.GapWorker.prefetchPositionWithDeadline(GapWorker.java:288)
    at androidx.recyclerview.widget.GapWorker.flushTaskWithDeadline(GapWorker.java:345)
    at androidx.recyclerview.widget.GapWorker.flushTasksWithDeadline(GapWorker.java:361)
    at androidx.recyclerview.widget.GapWorker.prefetch(GapWorker.java:368)
    at androidx.recyclerview.widget.GapWorker.run(GapWorker.java:399)
    at android.os.Handler.handleCallback(Handler.java:790)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:164)
    at android.app.ActivityThread.main(ActivityThread.java:6494)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)

my recycler view

    val ItemsViewModel = mList[position]
    holder.textViewActor.text = ItemsViewModel.actor
    holder.textViewCharacter.text = ItemsViewModel.character
    holder.textViewHouse.text = ItemsViewModel.house
    val picasso = Picasso.get()
    picasso.load(ItemsViewModel.imageUrl).into(holder.imageView)
}

CodePudding user response:

Your log clearly shows your error. Your ItemsViewModel.imageUrl is likely to return null or the empty string. please check that ItemsViewModel.imageUrl is empty or null before using Picasso to load the image.

val picasso = Picasso.get()
if(!ItemsViewModel.imageUrl.isNullOrEmpty()) {
    picasso.load().into(holder.imageView)
} else {
    // Show placeholders here.
}

CodePudding user response:

For null or empty urls, you can directly set a placeholder image. You should also provide placeholder drawable as fallback while image is loading or fails to load for any reason (slow internet, invalid urls etc).

if (ItemsViewModel.imageUrl.isNullOrEmpty()) {
   holder.imageView.setImageResource(R.drawable.placeholder);
} else {
   piccaso.load(image).placeholder(R.drawable.placeholder).into(holder.imageView);
}
  • Related