简体   繁体   English

片段中的 Listview 适配器给出空指针异常

[英]Listview adapter in a fragment giving null pointer exception

I made a listview in a fragment There is a null pointer exception for the adapter of the list view But the Arraylist and the adapter are not null.我在一个fragment里做了一个listview list view的adapter有空指针异常但是Arraylist和adapter都不是空的。

Error: lvElevator cannot be null错误:lvElevator 不能为空

The error is for the below line错误是针对以下行

lvElevator.adapter = EAdapter

Fragment Class片段类


var EList=ArrayList<E1List>()

class EDetails: Fragment() {

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        EList.add(E1List(1))
        EList.add(E1List(2))
        var EAdapter = EleAdapter(EList)
        lvElevator.adapter = EAdapter
        return inflater.inflate(R.layout.fragment_elevators, container, false)

    }

    inner class EleAdapter : BaseAdapter {
        var EListAdapter = ArrayList<E1List>()

        constructor(EListAdapter: ArrayList<E1List>) : super() {
            this.EListAdapter = EListAdapter
        }

        override fun getView(position: Int, convertView: View?, parent: ViewGroup?): View {
            var myView = layoutInflater.inflate(R.layout.elevator_ticket, null)
            var myEle = EListAdapter[position]
            myView.tvEnum.text = myEle.elevatornum.toString()
            return myView
        }

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

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

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

    }
}

You are trying to define lvElevator before fragment inflate the xml layout file.您正在尝试在片段膨胀 xml 布局文件之前定义 lvElevator。 There is two way to fix it.有两种方法可以修复它。

1) Use onViewCreated() method instead to load data into views. 1)使用onViewCreated()方法将数据加载到视图中。 And use onCreateView method to just inflate the view only.并使用 onCreateView 方法仅膨胀视图。

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
    EList.add(E1List(1))
    EList.add(E1List(2))
    var EAdapter = EleAdapter(EList)
    lvElevator.adapter = EAdapter
}

2) Modify onCreateView() method like below. 2)修改onCreateView()方法如下。

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {

    // Define views manually..
    val view = inflater.inflate(R.layout.fragment_elevators, container, false)
    val lvElevator = view.findViewById<ListView>(R.id.lvElevator)

    EList.add(E1List(1))
    EList.add(E1List(2))
    var EAdapter = EleAdapter(EList)
    lvElevator.adapter = EAdapter
    return view
}

As an experienced developer, I would recommended to use the first way as that method will only come in scope after layout completely inflated.作为一名经验丰富的开发人员,我建议使用第一种方法,因为该方法只有在布局完全膨胀后才会生效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Android Listview适配器在片段中提供了空指针异常 - Android Listview adapter giving null pointer exception in fragment RecyclerView 适配器在 Fragment 内给出 null 异常 - RecyclerView adapter giving null exception inside Fragment 带有片段空指针异常的 Android RecyclerView 适配器 - Android RecyclerView adapter with fragment null pointer exception 将适配器设置为列表视图时出现空指针异常 - Null pointer exception when setting adapter to listview 使用Fragment Android将自定义适配器设置为ListView时获取Null Pointer异常 - getting Null Pointer Exception on setting custom adapter to listview using Fragment Android listView.addHeaderView() 给出“空指针异常” - listView.addHeaderView() giving "null pointer exception" 片段活动内我的Listview适配器上的空指针 - Null pointer on my Listview Adapter within fragment activity 片段上的自定义列表的android空指针异常设置适配器 - android null pointer exception setting adapter for custom list on fragment 我的片段状态寻呼机适配器中的空指针异常 - Null Pointer Exception in my Fragment State Pager Adapter 使用GridView和自定义适配器的ViewPager片段中的空指针异常 - Null Pointer exception in ViewPager Fragment with GridView and custom Adapter
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM