你好 。当我将列表视图放在一个片段中时,该应用程序无法正常工作

时间:2018-10-31 18:15:49

标签: android kotlin

当我将列表视图放在一个片段中时,该应用程序无法正常工作。到目前为止,这是我的代码:

class BlankFragment : Fragment() {
    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
                              savedInstanceState: Bundle?): View? {
    // Inflate the layout for this fragment
    var i = inflater.inflate(R.layout.fragment_blank, container, false)
    var TheList      = arrayOf("Line1","Line2")
    var TheAdapter   = ArrayAdapter<String>(context, android.R.layout.simple_list_item_1, TheList)
    ListView.adapter = TheAdapter
    return i
}

1 个答案:

答案 0 :(得分:0)

如果您重写onViewCreated并将其初始化为此类,将会更好。

override fun onViewCreated(view:View?, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
var TheList = arrayOf("Line1","Line2")
var TheAdapter = ArrayAdapter<String>(context, android.R.layout.simple_list_item_1, TheList) ListView.adapter = TheAdapter
}

onviewCreated是您进行所有业务的地方。