0

应用程序崩溃null并在线抛出指针异常 RoomList.getAdapter().getCount()。我需要从中获取每个编辑文本值listview并对其应用一些计算,在搜索各种帖子后,我找到了这个解决方案,但它无法正常工作。

RoomList = (ListView) vi.findViewById(R.id.list2);
for (int i = 0; i < RoomList.getAdapter().getCount(); i++) {
        View view = RoomList.getChildAt(i);
        EditText edit = (EditText) view.findViewById(R.id.editRoom);
        Log.d("value from each edit text", edit.getText().toString());
}
4

2 回答 2

1

你有 NPE 因为RoomList.getAdapter()返回null. 也许你没有打电话RoomList.setAdapter(ListAdapter adapter)

顺便说一句,您应该将名称更改RoomListroomList, 以遵守 Java约定RoomList.getAdapter()乍一看像是某个RoomList类的静态方法调用。

于 2013-07-22T10:12:09.310 回答
0

问题是您在没有先设置它的情况下访问列表的适配器。

RoomList = (ListView) vi.findViewById(R.id.list2);
RoomList.setAdapter(new SubClassOfBaseAdapter()); // <-- setup your adapter here, with anArrayAdapter or a sublass of BaseAdapter
for (int i = 0; i < RoomList.getAdapter().getCount(); i++) {
    View view = RoomList.getChildAt(i);
    EditText edit = (EditText) view.findViewById(R.id.editRoom);
    Log.d("value from each edit text", edit.getText().toString());
}
于 2013-07-22T10:20:07.207 回答