Encontré un ejemplo de flexbox para android, ahora estoy intentando cargar una imagen con glide desde un adapter, sin embargo tengo un error de contexto.
class DivLikeAdapter(private val mItems: ArrayList<String>) : RecyclerView.Adapter<DivLikeAdapter.DivItemViewHolder>() {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): DivLikeAdapter.DivItemViewHolder {
val view = LayoutInflater.from(parent.context)
.inflate(R.layout.item, parent, false)
return DivItemViewHolder(view)
}
...
inner class DivItemViewHolder constructor(itemView: View) : RecyclerView.ViewHolder
(itemView) {
//private val text: TextView = itemView.findViewById(R.id.text)
private val img: ImageView = itemView.findViewById(R.id.imageView)
fun onBind(position: Int) {
//text.text = mItems[position]
Glide.with(MainActivity()) //AQUI
.load(mItems[position]).into(img)
}
}
}
ERROR
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com99.example.gal.flexboxlayout, PID: 23523
java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.Context android.content.Context.getApplicationContext()' on a null object reference
at android.content.ContextWrapper.getApplicationContext(ContextWrapper.java:118)
at com.bumptech.glide.Glide.get(Glide.java:188)
at com.bumptech.glide.Glide.getRetriever(Glide.java:774)
at com.bumptech.glide.Glide.with(Glide.java:826)
at com99.example.gal.flexboxlayout.DivLikeAdapter$DivItemViewHolder.onBind(DivLikeAdapter.kt:40)
at com99.example.gal.flexboxlayout.DivLikeAdapter.onBindViewHolder(DivLikeAdapter.kt:24)
at com99.example.gal.flexboxlayout.DivLikeAdapter.onBindViewHolder(DivLikeAdapter.kt:15)
at androidx.recyclerview.widget.RecyclerView$Adapter.onBindViewHolder(RecyclerView.java:6781)
....
El error es de contexto al parecer, aunque no estoy seguro.