I am trying to write a dialog box in my app . The problem is when i click the OK (SetPositiveButton) it is showing the following Runtime Error
E/AndroidRuntime(702): java.lang.ArrayIndexOutOfBoundsException
If i am trying to execute the commented lines in the below code , But it works fine in the onClick but showing some Kind of Bug for setPositiveButton
@Override
protected Dialog onCreateDialog(int id) {
switch(id){
case ONCLICK_EXTRAS:
return new AlertDialog.Builder(AcricklayoutActivity.this)
.setIcon(R.drawable.ic_launcher)
.setTitle("Extras")
.setSingleChoiceItems(R.array.extras, 0, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
String[] items = getResources().getStringArray(R.array.extras);
value = items[which];
Log.v("this",String.valueOf(which)+items[which]);
}
})
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// String[] items = getResources().getStringArray(R.array.extras);
//text.setText(items[which]);
// Log.v("this",String.valueOf(which)+items[which]);
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
}
})
.create();
I will explain my code here
- I am trying to use an String array from the Resource/values file to populate my AlertDialogbox ,
- And Storing that into the Static String variable value
it works fine till the positive button click of the dialog activity after that app gets crashed.. Can anyone Help me in this issue ???
Thanks in Advance !!
This
whichin this method tells the button which was clicked, and you should not be using it as index for your arrays.whichcan be any of these:Naturally if you use it for getting values from array, it will give you
ArrayIndexOutOfBoundsException