Java.lang.ArrayIndexOutOfBoundException - 警报对话框
Java.lang.ArrayIndexOutOfBoundException - AlertDialog
我正在尝试构建一个应用程序,如果用户单击一个按钮,它将显示一个带有 multiChoiceItems
、
的警告对话框
我从 ArrayList
(已转换为数组字符串)分配了 multiChoiceItems 中选项的值,但是当我尝试单击按钮时,程序出错而不是显示对话框选项,
它抛出一个 java.lang.ArrayIndexOutOfBoundException
异常,但我不知道错误在哪里以及我应该如何修复它。
这是我的一段代码 Logcat :
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.create_todolist);
sessionAndDeclaration();
getActionBar().setDisplayShowHomeEnabled(false);
new AttemptGetReceiver().execute();
}
class AttemptGetReceiver extends AsyncTask<Void, Void, Boolean> {
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
pd = new ProgressDialog(CreateToDoList.this);
pd.setMessage("Loading...");
pd.setIndeterminate(false);
pd.show();
pd.setCancelable(true);
}
// will get group member from selected group id to be receiver
@Override
protected Boolean doInBackground(Void... arg0) {
// TODO Auto-generated method stub
int success = 0;
try {
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("GroupId", groupId));
JSONObject json = jsonParser.makeHttpRequest(url, "POST",
params);
success = json.getInt("success");
if (success == 1) {
Log.d("Success", "Getting parsed data");
mMember = json.getJSONArray("members");
try {
for (int i = 0; i < mMember.length(); i++) {
JSONObject c = mMember.getJSONObject(i);
String parsedMember = c.getString("Username");
mMemberList.add(parsedMember);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
} catch (JSONException e) {
}
return null;
}
@Override
protected void onPostExecute(Boolean result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
pd.dismiss();
}
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.bChooseReceiver:
alertDialogListReceiver();
break;
}
public void alertDialogListReceiver() {
// TODO Auto-generated method stub
stringList.clear();
final String[] listMember = new String[mMemberList.size()];
final boolean[] itemsChecked = new boolean[mMemberList.size()];
for (int i = 0; i < mMemberList.size(); i++) {
listMember[i] = mMemberList.get(i);
}
stringList.clear();
AlertDialog.Builder builder = new AlertDialog.Builder(
CreateToDoList.this);
builder.setTitle("Choose Your Receiver : ");
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
xmlListReceiver.setText("Post To : ");
for (int i = 0; i < listMember.length; i++) {
if (itemsChecked[i]) {
stringList.add(listMember[i]);
itemsChecked[i] = false;
}
Log.d("flag", "after for");
}
String string = "";
for (int i = 0; i < stringList.size(); i++) {
xmlListReceiver.setText("Send To : ");
string = string + " " + stringList.get(i);
}
xmlListReceiver.setText(xmlListReceiver.getText().toString()
+ string);
}
});
builder.setMultiChoiceItems(listMember, new boolean[] { false, false,
false }, new DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialog, int which,
boolean isChecked) {
itemsChecked[which] = isChecked;
}
});
builder.show();
Log.d("flag", "bottom of alert dialog");
}
}
Logcat :
02-01 22:23:40.866: E/AndroidRuntime(28969): FATAL EXCEPTION: main
02-01 22:23:40.866: E/AndroidRuntime(28969): Process: com.thesis.teamizer, PID: 28969
02-01 22:23:40.866: E/AndroidRuntime(28969): java.lang.ArrayIndexOutOfBoundsException: length=3; index=3
02-01 22:23:40.866: E/AndroidRuntime(28969): at com.android.internal.app.AlertController$AlertParams.getView(AlertController.java:894)
02-01 22:23:40.866: E/AndroidRuntime(28969): at android.widget.AbsListView.obtainView(AbsListView.java:2712)
02-01 22:23:40.866: E/AndroidRuntime(28969): at android.widget.ListView.measureHeightOfChildren(ListView.java:1274)
02-01 22:23:40.866: E/AndroidRuntime(28969): at android.widget.ListView.onMeasure(ListView.java:1186)
02-01 22:23:40.866: E/AndroidRuntime(28969): at android.view.View.measure(View.java:17495)
02-01 22:23:40.866: E/AndroidRuntime(28969): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5363)
02-01 22:23:40.866: E/AndroidRuntime(28969): at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1410)
02-01 22:23:40.866: E/AndroidRuntime(28969): at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
02-01 22:23:40.866: E/AndroidRuntime(28969): at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
02-01 22:23:40.866: E/AndroidRuntime(28969): at android.view.View.measure(View.java:17495)
02-01 22:23:40.866: E/AndroidRuntime(28969): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5363)
02-01 22:23:40.866: E/AndroidRuntime(28969): at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1410)
02-01 22:23:40.866: E/AndroidRuntime(28969): at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
02-01 22:23:40.866: E/AndroidRuntime(28969): at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
02-01 22:23:40.866: E/AndroidRuntime(28969): at android.view.View.measure(View.java:17495)
02-01 22:23:40.866: E/AndroidRuntime(28969): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5363)
02-01 22:23:40.866: E/AndroidRuntime(28969): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
02-01 22:23:40.866: E/AndroidRuntime(28969): at android.view.View.measure(View.java:17495)
02-01 22:23:40.866: E/AndroidRuntime(28969): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5363)
02-01 22:23:40.866: E/AndroidRuntime(28969): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
02-01 22:23:40.866: E/AndroidRuntime(28969): at android.view.View.measure(View.java:17495)
02-01 22:23:40.866: E/AndroidRuntime(28969): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5363)
02-01 22:23:40.866: E/AndroidRuntime(28969): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
02-01 22:23:40.866: E/AndroidRuntime(28969): at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2548)
02-01 22:23:40.866: E/AndroidRuntime(28969): at android.view.View.measure(View.java:17495)
02-01 22:23:40.866: E/AndroidRuntime(28969): at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2285)
02-01 22:23:40.866: E/AndroidRuntime(28969): at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1371)
02-01 22:23:40.866: E/AndroidRuntime(28969): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1595)
02-01 22:23:40.866: E/AndroidRuntime(28969): at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1254)
02-01 22:23:40.866: E/AndroidRuntime(28969): at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6637)
02-01 22:23:40.866: E/AndroidRuntime(28969): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:814)
02-01 22:23:40.866: E/AndroidRuntime(28969): at android.view.Choreographer.doCallbacks(Choreographer.java:614)
02-01 22:23:40.866: E/AndroidRuntime(28969): at android.view.Choreographer.doFrame(Choreographer.java:584)
02-01 22:23:40.866: E/AndroidRuntime(28969): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:800)
02-01 22:23:40.866: E/AndroidRuntime(28969): at android.os.Handler.handleCallback(Handler.java:733)
02-01 22:23:40.866: E/AndroidRuntime(28969): at android.os.Handler.dispatchMessage(Handler.java:95)
02-01 22:23:40.866: E/AndroidRuntime(28969): at android.os.Looper.loop(Looper.java:146)
02-01 22:23:40.866: E/AndroidRuntime(28969): at android.app.ActivityThread.main(ActivityThread.java:5602)
02-01 22:23:40.866: E/AndroidRuntime(28969): at java.lang.reflect.Method.invokeNative(Native Method)
02-01 22:23:40.866: E/AndroidRuntime(28969): at java.lang.reflect.Method.invoke(Method.java:515)
02-01 22:23:40.866: E/AndroidRuntime(28969): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283)
02-01 22:23:40.866: E/AndroidRuntime(28969): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
02-01 22:23:40.866: E/AndroidRuntime(28969): at dalvik.system.NativeStart.main(Native Method)
我认为问题在于您在此处设置了默认项目状态(选中或未选中):
builder.setMultiChoiceItems(listMember, new boolean[] { false, false,
false }, new DialogInterface.OnMultiChoiceClickListener() {
3 件商品,但您可以拥有不同数量的商品:mMemberList.size()
,来自 javadoc:
checkedItems specifies which items are checked. It should be null in which case no items are checked. If non null it must be exactly the same length as the array of items.
对 checkedItems
使用 null
应该可以解决问题:
builder.setMultiChoiceItems(listMember, null, new DialogInterface.OnMultiChoiceClickListener() {
我正在尝试构建一个应用程序,如果用户单击一个按钮,它将显示一个带有 multiChoiceItems
、
我从 ArrayList
(已转换为数组字符串)分配了 multiChoiceItems 中选项的值,但是当我尝试单击按钮时,程序出错而不是显示对话框选项,
它抛出一个 java.lang.ArrayIndexOutOfBoundException
异常,但我不知道错误在哪里以及我应该如何修复它。
这是我的一段代码 Logcat :
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.create_todolist);
sessionAndDeclaration();
getActionBar().setDisplayShowHomeEnabled(false);
new AttemptGetReceiver().execute();
}
class AttemptGetReceiver extends AsyncTask<Void, Void, Boolean> {
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
pd = new ProgressDialog(CreateToDoList.this);
pd.setMessage("Loading...");
pd.setIndeterminate(false);
pd.show();
pd.setCancelable(true);
}
// will get group member from selected group id to be receiver
@Override
protected Boolean doInBackground(Void... arg0) {
// TODO Auto-generated method stub
int success = 0;
try {
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("GroupId", groupId));
JSONObject json = jsonParser.makeHttpRequest(url, "POST",
params);
success = json.getInt("success");
if (success == 1) {
Log.d("Success", "Getting parsed data");
mMember = json.getJSONArray("members");
try {
for (int i = 0; i < mMember.length(); i++) {
JSONObject c = mMember.getJSONObject(i);
String parsedMember = c.getString("Username");
mMemberList.add(parsedMember);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
} catch (JSONException e) {
}
return null;
}
@Override
protected void onPostExecute(Boolean result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
pd.dismiss();
}
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.bChooseReceiver:
alertDialogListReceiver();
break;
}
public void alertDialogListReceiver() {
// TODO Auto-generated method stub
stringList.clear();
final String[] listMember = new String[mMemberList.size()];
final boolean[] itemsChecked = new boolean[mMemberList.size()];
for (int i = 0; i < mMemberList.size(); i++) {
listMember[i] = mMemberList.get(i);
}
stringList.clear();
AlertDialog.Builder builder = new AlertDialog.Builder(
CreateToDoList.this);
builder.setTitle("Choose Your Receiver : ");
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
xmlListReceiver.setText("Post To : ");
for (int i = 0; i < listMember.length; i++) {
if (itemsChecked[i]) {
stringList.add(listMember[i]);
itemsChecked[i] = false;
}
Log.d("flag", "after for");
}
String string = "";
for (int i = 0; i < stringList.size(); i++) {
xmlListReceiver.setText("Send To : ");
string = string + " " + stringList.get(i);
}
xmlListReceiver.setText(xmlListReceiver.getText().toString()
+ string);
}
});
builder.setMultiChoiceItems(listMember, new boolean[] { false, false,
false }, new DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialog, int which,
boolean isChecked) {
itemsChecked[which] = isChecked;
}
});
builder.show();
Log.d("flag", "bottom of alert dialog");
}
}
Logcat :
02-01 22:23:40.866: E/AndroidRuntime(28969): FATAL EXCEPTION: main
02-01 22:23:40.866: E/AndroidRuntime(28969): Process: com.thesis.teamizer, PID: 28969
02-01 22:23:40.866: E/AndroidRuntime(28969): java.lang.ArrayIndexOutOfBoundsException: length=3; index=3
02-01 22:23:40.866: E/AndroidRuntime(28969): at com.android.internal.app.AlertController$AlertParams.getView(AlertController.java:894)
02-01 22:23:40.866: E/AndroidRuntime(28969): at android.widget.AbsListView.obtainView(AbsListView.java:2712)
02-01 22:23:40.866: E/AndroidRuntime(28969): at android.widget.ListView.measureHeightOfChildren(ListView.java:1274)
02-01 22:23:40.866: E/AndroidRuntime(28969): at android.widget.ListView.onMeasure(ListView.java:1186)
02-01 22:23:40.866: E/AndroidRuntime(28969): at android.view.View.measure(View.java:17495)
02-01 22:23:40.866: E/AndroidRuntime(28969): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5363)
02-01 22:23:40.866: E/AndroidRuntime(28969): at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1410)
02-01 22:23:40.866: E/AndroidRuntime(28969): at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
02-01 22:23:40.866: E/AndroidRuntime(28969): at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
02-01 22:23:40.866: E/AndroidRuntime(28969): at android.view.View.measure(View.java:17495)
02-01 22:23:40.866: E/AndroidRuntime(28969): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5363)
02-01 22:23:40.866: E/AndroidRuntime(28969): at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1410)
02-01 22:23:40.866: E/AndroidRuntime(28969): at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
02-01 22:23:40.866: E/AndroidRuntime(28969): at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
02-01 22:23:40.866: E/AndroidRuntime(28969): at android.view.View.measure(View.java:17495)
02-01 22:23:40.866: E/AndroidRuntime(28969): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5363)
02-01 22:23:40.866: E/AndroidRuntime(28969): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
02-01 22:23:40.866: E/AndroidRuntime(28969): at android.view.View.measure(View.java:17495)
02-01 22:23:40.866: E/AndroidRuntime(28969): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5363)
02-01 22:23:40.866: E/AndroidRuntime(28969): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
02-01 22:23:40.866: E/AndroidRuntime(28969): at android.view.View.measure(View.java:17495)
02-01 22:23:40.866: E/AndroidRuntime(28969): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5363)
02-01 22:23:40.866: E/AndroidRuntime(28969): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
02-01 22:23:40.866: E/AndroidRuntime(28969): at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2548)
02-01 22:23:40.866: E/AndroidRuntime(28969): at android.view.View.measure(View.java:17495)
02-01 22:23:40.866: E/AndroidRuntime(28969): at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2285)
02-01 22:23:40.866: E/AndroidRuntime(28969): at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1371)
02-01 22:23:40.866: E/AndroidRuntime(28969): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1595)
02-01 22:23:40.866: E/AndroidRuntime(28969): at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1254)
02-01 22:23:40.866: E/AndroidRuntime(28969): at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6637)
02-01 22:23:40.866: E/AndroidRuntime(28969): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:814)
02-01 22:23:40.866: E/AndroidRuntime(28969): at android.view.Choreographer.doCallbacks(Choreographer.java:614)
02-01 22:23:40.866: E/AndroidRuntime(28969): at android.view.Choreographer.doFrame(Choreographer.java:584)
02-01 22:23:40.866: E/AndroidRuntime(28969): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:800)
02-01 22:23:40.866: E/AndroidRuntime(28969): at android.os.Handler.handleCallback(Handler.java:733)
02-01 22:23:40.866: E/AndroidRuntime(28969): at android.os.Handler.dispatchMessage(Handler.java:95)
02-01 22:23:40.866: E/AndroidRuntime(28969): at android.os.Looper.loop(Looper.java:146)
02-01 22:23:40.866: E/AndroidRuntime(28969): at android.app.ActivityThread.main(ActivityThread.java:5602)
02-01 22:23:40.866: E/AndroidRuntime(28969): at java.lang.reflect.Method.invokeNative(Native Method)
02-01 22:23:40.866: E/AndroidRuntime(28969): at java.lang.reflect.Method.invoke(Method.java:515)
02-01 22:23:40.866: E/AndroidRuntime(28969): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283)
02-01 22:23:40.866: E/AndroidRuntime(28969): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
02-01 22:23:40.866: E/AndroidRuntime(28969): at dalvik.system.NativeStart.main(Native Method)
我认为问题在于您在此处设置了默认项目状态(选中或未选中):
builder.setMultiChoiceItems(listMember, new boolean[] { false, false,
false }, new DialogInterface.OnMultiChoiceClickListener() {
3 件商品,但您可以拥有不同数量的商品:mMemberList.size()
,来自 javadoc:
checkedItems specifies which items are checked. It should be null in which case no items are checked. If non null it must be exactly the same length as the array of items.
对 checkedItems
使用 null
应该可以解决问题:
builder.setMultiChoiceItems(listMember, null, new DialogInterface.OnMultiChoiceClickListener() {