在Adapter.getView Kotlin android上参数convertView上的IllegalArgumentException

时间:2018-06-05 17:05:54

标签: android listview kotlin

java.lang.IllegalArgumentException: Parameter specified as non-null is null: method kotlin.jvm.internal.Intrinsics.checkParameterIsNotNull, parameter convertView
Adapter.getView
 at android.widget.AbsListView.obtainView(AbsListView.java:2346)
        at android.widget.ListView.makeAndAddView(ListView.java:1876)
        at android.widget.ListView.fillDown(ListView.java:702)
        at android.widget.ListView.fillFromTop(ListView.java:763)
        at android.widget.ListView.layoutChildren(ListView.java:1671)
        at android.widget.AbsListView.onLayout(AbsListView.java:2148)

这是android的logcat。 我试过java,它在Adapter或其他工作正常的基本适配器有问题。 我尝试使用public constructor并且还检查了数组列表计数3。它总是在getView

崩溃

MyAdapter Code ::

   inner class MyAppAdapter constructor(private val parkingList: ArrayList<App>, private val mContext: Context) : BaseAdapter() {

        override fun getCount(): Int {
            return this.parkingList.size
        }

        override fun getItem(position: Int): Any {
            return position
        }

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

        override fun getView(position: Int, convertView: View, parent: ViewGroup): View? {
            val viewHolder: ViewHolder
            var rowView: View? = convertView
            if (rowView == null) {
                rowView = LayoutInflater.from(mContext).inflate(R.layout.item_more_apps, parent, false)
                viewHolder = ViewHolder()
                viewHolder.appIcon = rowView.findViewById(R.id.appIcon)
                viewHolder.appName = rowView.findViewById(R.id.appName)
                viewHolder.appDescription = rowView.findViewById(R.id.appDescription)
                rowView.tag = viewHolder
            } else {
                viewHolder = convertView.tag as ViewHolder
            }
            viewHolder.appName!!.text = String.format("%s", this.parkingList[position].name)
            viewHolder.appDescription!!.text = String.format("%s", this.parkingList[position].description)
            Glide.with(applicationContext).load(this.parkingList[position].icon).into(viewHolder.appIcon!!)
            rowView?.setOnClickListener {
                try {
                    startActivity(Intent("android.intent.action.VIEW", Uri.parse("market://details?id=" + this@MyAppAdapter.parkingList[position].link)))
                } catch (e: ActivityNotFoundException) {
                    startActivity(Intent("android.intent.action.VIEW", Uri.parse("http://play.google.com/store/apps/details?id=" + this@MyAppAdapter.parkingList[position].link)))
                }
            }
            return rowView
        }

        inner class ViewHolder {
            var appDescription: TextView? = null
            var appIcon: ImageView? = null
            var appName: TextView? = null
        }
    }

用于AsyncTask - &gt; onPostExecute

  myAppAdapter = MyAppAdapter(appArrayList, applicationContext)
            lvPoses!!.adapter = myAppAdapter

变量像这样解除

   lateinit var myAppAdapter: MyAppAdapter
    private val appArrayList = ArrayList<App>()
    private var lvPoses: ListView? = null

2 个答案:

答案 0 :(得分:7)

如果尚未创建视图,则

convertView可以为null。修复参数声明:

override fun getView(position: Int, convertView: View?, parent: 
     ViewGroup): View? {
...
}

答案 1 :(得分:0)

我和Asynctasks有类似的问题,不确定这是否会对你有所帮助,但是如果你只是执行asynctask(没有.get())。调用asynctask后你的代码会运行,但是因为Asynctask还没有执行,所以元素仍然是NULL,当它发生时它已经太晚了。尝试在执行后添加.get()(它将冻结UI,但会及时调用postExecute方法)。或者另一种方法是设置一个布尔变量false,并在执行post执行时将其设置为true,然后每隔一秒左右检查一次(Timer,Thread),如果变量为true,则执行其余的代码。为了获得更好的UI体验,请在preExecutepostExecute之间添加progressdialog。

或者您也可以将视图设置为NULL,但我不认为这可以修复您的问题,因为您不希望它为NULL。