当我单击复选框时,该对话框可以正常工作,但是当我再次单击并取消选中当前选中的项目时,活动崩溃。如何正确编码呢?我仍然对Android还是陌生的,因为我只在空闲时间这样做。
代码:
mAllergies.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final AlertDialog.Builder mBuilder = new AlertDialog.Builder(DietGeneration.this);
mBuilder.setTitle("Please select from the list of allergies below.");
mBuilder.setMultiChoiceItems(listItems, checkedItems, new DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialog, int position, boolean isChecked) {
if (isChecked) {
if(!mUserItems.contains(position)) {
mUserItems.add(position);
}else{
mUserItems.remove(position);
}
} else{
if(mUserItems.contains(position)) {
mUserItems.remove(position);
}
}
}
});
mBuilder.setCancelable(false);
mBuilder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
for (int i = 0; i < mUserItems.size(); i++) {
item = item + listItems[mUserItems.get(i)];
if (i != mUserItems.size() - 1) {
item = item + ", ";
}
}
mItemSelected.setText(item);
if (mUserItems.isEmpty()) {
mUserItems.clear();
mAllergies.setChecked(false);
} else {
mAllergies.setChecked(true);
}
}
});
mBuilder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int i) {
if (mUserItems.isEmpty()) {
mUserItems.clear();
mAllergies.setChecked(false);
} else {
mAllergies.setChecked(false);
}
dialog.dismiss();
}
});
mBuilder.setNeutralButton("Clear All", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
for (int i = 0; i < checkedItems.length; i++) {
checkedItems[i] = false;
mUserItems.clear();
mItemSelected.setText("");
if (mItemSelected.getText() == "") {
mAllergies.setChecked(false);
} else {
mAllergies.setChecked(false);
}
}
}
});
AlertDialog mDialog = mBuilder.create();
mDialog.show();
}
});
LOGCAT:
java.lang.IndexOutOfBoundsException: Invalid index 1, size is 1
at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:260)
at java.util.ArrayList.remove(ArrayList.java:408)
at com.example.mgimoro.dietgo.DietGeneration$3$1.onClick(DietGeneration.java:93)
at android.support.v7.app.AlertController$AlertParams$4.onItemClick(AlertController.java:1080)
at android.widget.AdapterView.performItemClick(AdapterView.java:319)
at android.widget.AbsListView.performItemClick(AbsListView.java:1209)
at android.widget.AbsListView$PerformClick.run(AbsListView.java:3290)
at android.widget.AbsListView.onTouchUp(AbsListView.java:4299)
at android.widget.HwAbsListView.onTouchUp(HwAbsListView.java:465)
at android.widget.AbsListView.onTouchEvent(AbsListView.java:3998)
at android.view.View.dispatchTouchEvent(View.java:9480)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2662)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2304)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2668)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2318)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2668)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2318)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2668)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2318)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2668)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2318)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2668)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2318)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2668)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2318)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2668)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2318)
at com.android.internal.policy.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:2512)
at com.android.internal.policy.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1813)
at android.app.Dialog.dispatchTouchEvent(Dialog.java:843)
at android.support.v7.view.WindowCallbackWrapper.dispatchTouchEvent(WindowCallbackWrapper.java:68)
at com.android.internal.policy.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:2473)
at android.view.View.dispatchPointerEvent(View.java:9704)