在ListView中获取空指针异常setMultiChoiceListener方法Android

时间:2015-06-07 09:54:19

标签: android listview nullpointerexception

我想用{Class}类的联系人填充listview,其中包含人名,电话号码和图片。我已经设法创建了一个我需要实现的东西,它是一个'删除'功能,通过长按项目,我从操作栏中选择删除选项。一切正常,除非我选择列表中的最后一项,我在setBackgroundColor(getResources()...)部分得到一个空指针异常。我已将“try .... catch”包含在代码的if部分中,但列表中最后一项的选择和删除操作仍无效。< / p>

if (checked) {
                try {
                    listView.getChildAt(position).setBackgroundColor(getResources().getColor(R.color.darkblue));
                    Contact prsntoArray = (Contact) personDlt;
                    dltPersonsList.add(prsntoArray);
                } catch (NullPointerException e) {
                    e.printStackTrace();
                }
            } else {
                listView.getChildAt(position).setBackgroundColor(getResources().getColor(R.color.abc_background_cache_hint_selector_material_light));
                dltPersonsList.remove(personDlt);

            }

这是应用程序的整个onCreate方法。

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.show_contacts_listview);
    final ListView listView = (ListView) findViewById(R.id.listview);

    DatabaseHandler db = new DatabaseHandler(getApplicationContext());
    List<Contact> contactList = db.getAllContacts();
    listAdapter = new ListAdapter(this, R.layout.contact_view_item,contactList);
    listView.setAdapter(listAdapter);
    listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);

    listView.setMultiChoiceModeListener(new AbsListView.MultiChoiceModeListener() {
        @Override
        public void onItemCheckedStateChanged(ActionMode mode, int position, long id, boolean checked) {
            // Capture total checked items
            final int checkedCount = listView.getCheckedItemCount();
            // Set the CAB title according to total checked items
            mode.setTitle(checkedCount + " Selected");
            personDlt = listView.getItemAtPosition(position);
            if (checked) {
                try {
                    listView.getChildAt(position).setBackgroundColor(getResources().getColor(R.color.darkblue));
                    Contact prsntoArray = (Contact) personDlt;
                    dltPersonsList.add(prsntoArray);
                } catch (NullPointerException e) {
                    e.printStackTrace();
                }
            } else {
                listView.getChildAt(position).setBackgroundColor(getResources().getColor(R.color.abc_background_cache_hint_selector_material_light));
                dltPersonsList.remove(personDlt);

            }
            // Calls toggleSelection method from ListViewAdapter Class
            listAdapter.toggleSelection(position);
        }

        @Override
        public boolean onCreateActionMode(ActionMode mode, Menu menu) {
            mode.getMenuInflater().inflate(R.menu.menu_main, menu);
            return true;
        }

        @Override
        public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
            return false;
        }

        // Called when the user selects a contextual menu item
        @Override
        public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
            switch (item.getItemId()) {
                case R.id.delete:
                    SparseBooleanArray selected = listAdapter.getSelectedIds();
                    for (int i = (selected.size() - 1); i >= 0; i--) {
                        if (selected.valueAt(i)) {
                            Contact selecteditem = listAdapter
                                    .getItem(selected.keyAt(i));
                            // Remove selected items following the ids
                            listAdapter.remove(selecteditem);
                        }
                    }
                    Iterator<Contact> dltIter = dltPersonsList.iterator();
                    do {

                        Contact dltContct = dltIter.next();
                        DatabaseHandler db = new DatabaseHandler(getApplicationContext());
                        db.deleteContact(dltContct);

                    } while (dltIter.hasNext());


                    // Close CAB
                    mode.finish();
                    return true;
                default:
                    return false;
            }

        }

        @Override
        public void onDestroyActionMode(ActionMode mode) {
            listAdapter.removeSelection();

        }
    });}

1 个答案:

答案 0 :(得分:0)

您在单个getChildAt电话上添加了试用版。

您应该只进行空检查:

if (listView.getChildAt(position) != null) {
    if (checked) {
        listView.getChildAt(position)
                .setBackgroundColor(getResources().getColor(R.color.darkblue));
        Contact prsntoArray = (Contact) personDlt;
        dltPersonsList.add(prsntoArray);
    } else {
        listView.getChildAt(position).setBackgroundColor(getResources()
                .getColor(R.color.abc_background_cache_hint_selector_material_light));
        dltPersonsList.remove(personDlt);
    }
}

请记住,ListView会重新使用视图,因此在滚动时,背景可能会应用于其他项目。当然,如果您的适配器中没有任何管理这些状态的特定代码。然而,这超出了这个问题的范围。