运行时错误使用ListView Android与Kotlin?

class MainActivity : AppCompatActivity() { var data = listOf("a", "b", "c", "d", "e", "f", "g", "h", "i") override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) val mAdapter = MAdapter(this,R.layout.mitem, data) setContentView(R.layout.activity_main) listView.adapter = mAdapter } } class MAdapter(context: Context, var id: Int, objects: List<String>) : ArrayAdapter<String>(context, id, objects) { override fun getView(pos: Int, cv: View, pa: ViewGroup): View { val mItem = getItem(pos) val v = LayoutInflater.from(context).inflate(id, pa, false) v.itemid.text = mItem v.itemdes.text = "common description" return v } } 

我认为布局文件没有问题。 它在编译过程中没有显示警告/错误。 在设备上运行时会崩溃。

logcat在这里

 07-31 17:58:38.470 30321-30321/? E/dalvikvm: >>>>> Normal User 07-31 17:58:38.470 30321-30321/? E/dalvikvm: >>>>> com.lukhy.landroid [ userId:0 | appId:10211 ] 07-31 17:58:50.950 30321-30321/com.lukhy.landroid E/dalvikvm: Could not find class 'android.graphics.drawable.RippleDrawable', referenced from method android.support.v7.widget.AppCompatImageHelper.hasOverlappingRendering 07-31 17:58:51.110 30321-30321/com.lukhy.landroid E/AndroidRuntime: FATAL EXCEPTION: main Process: com.lukhy.landroid, PID: 30321 java.lang.IllegalArgumentException: Parameter specified as non-null is null: method kotlin.jvm.internal.Intrinsics.checkParameterIsNotNull, parameter cv at com.lukhy.landroid.MAdapter.getView(MainActivity.kt) at android.widget.AbsListView.obtainView(AbsListView.java:2753) at android.widget.ListView.measureHeightOfChildren(ListView.java:1274) at android.widget.ListView.onMeasure(ListView.java:1186) at android.view.View.measure(View.java:17442) 

只需修改getView方法签名,如下所示:

 cv: View? 

重写getView(pos:Int,cv:View ?, pa:ViewGroup):View

在JAVA中, converView对象是Nullable 。 当第一次创建列表项时它将为空。

从这里下载源代码( Kotlin Android中的ListView )

MainActivity.kt:

 package com.deepshikha.listviewinkotlin import android.os.Bundle import android.support.v7.app.AppCompatActivity import kotlinx.android.synthetic.main.activity_main.* public class MainActivity : AppCompatActivity() { override fun onCreate(savedState: Bundle?) { super.onCreate(savedState) setContentView(R.layout.activity_main) val al_flower=ArrayList<Model_flower>() al_flower.add(Model_flower("Rosa",R.drawable.rosa,"A rose is a woody perennial flowering plant of the genus Rosa, in the family Rosaceae, or the flower it bears. There are over a hundred species and thousands of cultivars")) al_flower.add(Model_flower("Lotus",R.drawable.lotus,"Nelumbo nucifera, also known as Indian lotus, sacred lotus, bean of India, Egyptian bean or simply lotus, is one of two extant species of aquatic plant in the family Nelumbonaceae.")) al_flower.add(Model_flower("Cherry Blossom",R.drawable.nelumbo,"A cherry blossom (or commonly known in Japan as sakura) is the flower of any of several trees of genus Prunus, particularly the Japanese cherry, Prunus serrulata")) al_flower.add(Model_flower("Bird of Paradise",R.drawable.birdofparadise,"The birds-of-paradise are members of the family Paradisaeidae of the order Passeriformes. The majority of species are found in eastern Indonesia, Papua New Guinea, and eastern Australia. The family has 42 species in 15 genera")) al_flower.add(Model_flower("Tulips",R.drawable.tulips,"The tulip is a Eurasian and North African genus of herbaceous, perennial, bulbous plants in the lily family, with showy flowers. About 75 wild species are accepted")) al_flower.add(Model_flower("Dahlia",R.drawable.dahlia,"Dahlia is a genus of bushy, tuberous, herbaceous perennial plants native to Mexico. A member of the Asteraceae, dicotyledonous plants, related species include the sunflower, daisy, chrysanthemum, and zinnia.")) al_flower.add(Model_flower("Water Lilies",R.drawable.waterlilies,"Lilium is a genus of herbaceous flowering plants growing from bulbs, all with large prominent flowers. Lilies are a group of flowering plants which are important in culture and literature in much of the world.")) al_flower.add(Model_flower("Gazania",R.drawable.gazania,"Gazania is a genus of flowering plants in the family Asteraceae, native to Southern Africa. They produce large, daisy-like composite flowerheads in brilliant shades of yellow and orange, over a long period in summer.")) al_flower.add(Model_flower("Orchid",R.drawable.orchid,"The Orchidaceae are a diverse and widespread family of flowering plants, with blooms that are often colourful and fragrant, commonly known as the orchid family. Along with the Asteraceae, they are one of the two largest families of flowering plants")) val obj_adapter : CustomAdapter obj_adapter = CustomAdapter(applicationContext,al_flower) lv_flower.adapter=obj_adapter } 

}

CustomAdapter.kt:

 package com.deepshikha.listviewinkotlin import android.content.Context import android.view.LayoutInflater import android.view.View import android.view.ViewGroup import android.widget.BaseAdapter import android.widget.ImageView import android.widget.TextView /** * Created by deepshikha on 31/7/17. */ class CustomAdapter(context: Context,al_flower:ArrayList<Model_flower>) : BaseAdapter(){ private val mInflator: LayoutInflater private val al_flower:ArrayList<Model_flower> init { this.mInflator = LayoutInflater.from(context) this.al_flower=al_flower } override fun getCount(): Int { return al_flower.size } override fun getItem(position: Int): Any { return al_flower.get(position) } override fun getItemId(position: Int): Long { return position.toLong() } override fun getView(position: Int, convertView: View?, parent: ViewGroup): View? { val view: View? val vh: ListRowHolder if (convertView == null) { view = this.mInflator.inflate(R.layout.adapter_layout, parent, false) vh = ListRowHolder(view) view.tag = vh } else { view = convertView vh = view.tag as ListRowHolder } vh.label.text = al_flower.get(position).str_name vh.tv_des.text = al_flower.get(position).str_des vh.iv_image.setImageResource(al_flower.get(position).int_image) return view } } private class ListRowHolder(row: View?) { public val label: TextView public val tv_des: TextView public val iv_image: ImageView init { this.label = row?.findViewById<TextView>(R.id.tv_name) as TextView this.tv_des = row?.findViewById<TextView>(R.id.tv_des) as TextView this.iv_image = row?.findViewById<ImageView>(R.id.iv_flower) as ImageView } } 

谢谢!