2016-02-25 51 views
5

我已經擴展ArrayAdapter的微調:參數爲空在ArrayAdaper

class OrderAdapter(context: Context, resource: Int, objects: List<Order>) : ArrayAdapter<Order>(context, resource, objects) { 
     override fun getView(position: Int, convertView: View?, parent: ViewGroup): View? { 
      val view = super.getView(position, convertView, parent) 
      view?.let { view.find<TextView>(android.R.id.text1).text = getItem(position).name } 
      return view 
     } 
     override fun getDropDownView(position: Int, convertView: View?, parent: ViewGroup): View? { 
      val view = super.getDropDownView(position, convertView, parent) 
      view?.let {view.find<TextView>(android.R.id.text1).text = getItem(position).name } 
      return view 
     } 
    } 

我得到異常:

java.lang.IllegalArgumentException: Parameter specified as non-null is null: method kotlin.jvm.internal.Intrinsics.checkParameterIsNotNull, parameter convertView 
                          at com.github.blabla.endlesss.ui.adapter.OrderAdapter.getView(OrderAdapter.kt:0) 

任何想法如何解決呢?

+1

異常很清楚。您正在爲convertView參數傳遞null。 – Arno

+1

此方法由Android框架調用,參數實際上定義爲可爲空,因此應允許空值。 –

+0

@Dawid你確定你的代碼和你在這裏發佈的代碼是一樣的嗎?具體來說,'convertView'參數是否真的被聲明爲可空? –

回答

4

此問題是由增量式編譯引起的。只需要在從Java轉換之後重建項目。

0

請檢查下面的示例:

override fun getDropDownView(position: Int, convertView: View?, parent: ViewGroup): View? { 
    return getCustomView(position, convertView, parent) 
} 

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

fun getCustomView(position: Int, convertView: View?, parent: ViewGroup): View? { 
    val rootView: View? = LayoutInflater.from(context).inflate(R.layout.my_custom_view, parent, false) 
    return rootView 
} 

返回的值應爲空。

+1

這是由增量編譯引起的。 –

+0

@DawidHyży:請發表您的評論作爲回答並接受。在這裏發生了同樣的問題https://stackoverflow.com/questions/44800225/variable-is-declared-as-var-but-compiler-is-saying-it-val/44801687#44801687 –

-1
override fun getView(i: Int, view: View, parent: ViewGroup): View { 

var itemView = LayoutInflater.from(context).inflate(R.layout.item_dashbord_data, parent, false) 

     return itemView 
    } 

//更改View對象(itemView)的名稱與getView第二個對象(view)不同。因此將視圖更改爲itemView或其他名稱

+0

這已經解決了。 –