在listView

时间:2015-12-14 16:07:35

标签: java android android-listview

我在这一行得到了错误:

alert.setPositiveButton("YES", new DialogInterface.OnClickListener()

Logcat提供此错误

  

显示java.lang.NullPointerException

任何人都可以帮助我?

//method to remove list item
private void removeItemFromList(int position) {

    final int deletePosition = position;

    AlertDialog.Builder alert = new AlertDialog.Builder(MainActivity.this);

    alert.setTitle("Delete");
    alert.setMessage("Do you want delete this item?");

    alert.setPositiveButton("YES", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            // TOD O Auto-generated method stub

            // main code on after clicking yes
            arr.remove(deletePosition);
            adapter.notifyDataSetChanged();
            adapter.notifyDataSetInvalidated();

        }
    });
    alert.setNegativeButton("CANCEL", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            dialog.dismiss();
        }
    });
    alert.show();

}

1 个答案:

答案 0 :(得分:0)

Call removeAt(deletePosition) onClick of Yes

public void removeAt(int deletePosition) {
            arr.remove(deletePosition);
            notifyItemRemoved(deletePosition);
            notifyItemRangeChanged(deletePosition, arr.size());
        }