我在这一行得到了错误:
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();
}
答案 0 :(得分:0)
Call removeAt(deletePosition)
onClick of Yes
public void removeAt(int deletePosition) {
arr.remove(deletePosition);
notifyItemRemoved(deletePosition);
notifyItemRangeChanged(deletePosition, arr.size());
}