GridView适配器出现问题

问题描述

我的GridView适配器,只需在gridView项中添加一张图片

class GridPhotoAdapter(context: Context) : BaseAdapter() {
    private var imageArrayList = arrayOf(
        com.example.b_my_friend.R.mipmap.ic_launcher)

    private lateinit var inflater: LayoutInflater
    private val context: Context = context

    override fun getCount(): Int {
        return imageArrayList.size
    }
    override fun getItem(position: Int): Any {
        return imageArrayList[position]
    }

    override fun getItemId(position: Int): Long {
        return imageArrayList[position].toLong()
    }

    override fun getView(position: Int,convertView: View,parent: ViewGroup?): View {
        var view: View = convertView
        if (inflater == null){
            inflater = context!!.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
        }
        if (view == null) {
            view = inflater.inflate(com.example.b_my_friend.R.layout.item_grid_photo,null)
        }

        view.photo.setimageResource(imageArrayList[position])
        view
    }

}

当我在MainFragment中初始化

val gridPhotoAdapter = GridPhotoAdapter(activity!!)
gridPhoto.adapter = gridPhotoAdapter

我发现错误,我不知道为什么

gridPhoto不能为空

如何解决错误

fragment_photo.xml与我的GridView(gridPhoto)

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".ui.page.PhotoFragment">
    <GridView
        android:id="@+id/gridPhoto"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:columnWidth="30dp"
        android:numColumns="3"
        android:stretchMode="columnWidth"/>
</LinearLayout>

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...