Java.lang.ArrayIndexOutOfBoundException - AlertDialog

redRabbit

I'm trying to build an app where if user click a button, it will shows an alertdialog with multiChoiceItems,

I assigned the value of the option in multiChoiceItems from an ArrayList (which has been converted to arrayed string), but when I tried to click the button, the program is error instead of showing the dialog with the options,

It throws a java.lang.ArrayIndexOutOfBoundException exception, but I don't know where is the error and how I should fixed it.

Here is my piece of code and 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$1.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)
user180100

I think the problem is that you set default item state (checked or not) here:

builder.setMultiChoiceItems(listMember, new boolean[] { false, false,
        false }, new DialogInterface.OnMultiChoiceClickListener() {

for 3 items, but you can have a different number of item: mMemberList.size(), from the 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.

Using null for checkedItems should fix the issue:

builder.setMultiChoiceItems(listMember, null, new DialogInterface.OnMultiChoiceClickListener() {

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Android fragments alertDialog java.lang.IllegalStateException

From Dev

JAVA: ArrayIndexOutOFBoundException while reading imported txt

From Dev

JAVA: ArrayIndexOutOFBoundException while reading imported txt

From Dev

NullPointerException: Attempt to invoke virtual method AlertDialog.setTitle(java.lang.CharSequence) on a null object reference

From Dev

Android: custom AlertDialog, viewPager : java.lang.IllegalArgumentException: No view found for id

From Dev

ListView with imageView and TextView in AlertDialog leeaks memory and get Java.Lang.OutOfMemoryError

From Dev

ArrayIndexOutOfBoundException in HashMap

From Dev

Android-Picasso java.lang.IllegalArgumentException: Target must not be null. when the image view is in a custom layout for a alertdialog

From Dev

ArrayIndexOutofBoundException - Java - Android - Receiving streaming data and storing in multi-dimensional array Java

From Dev

Adding JCombobox in JTable - ArrayIndexOutOfBoundException

From Dev

ArrayIndexOutOfBoundException with set number of elements

From Dev

Adding JCombobox in JTable - ArrayIndexOutOfBoundException

From Dev

program keeps throwing an ArrayIndexOutofBoundException

From Dev

ArrayIndexOutOfBoundException vs IndexOutOfBoundException

From Dev

Java, Calling an AlertDialog from multiple classes

From Dev

java.lang.ClassNotFoundException: A

From Dev

"java.lang.NoClassDefFoundError"

From Dev

java.lang.NullPointerException?

From Dev

java.lang.NoSuchFieldException

From Dev

Java.lang.StringIndexOutOfBoundsException

From Dev

java.lang.ArrayIndexOutOfBoundsException

From Dev

Java - java.lang.NoSuchMethodException

From Dev

java.lang.NoClassDefFoundError: groovy/lang/GroovyObject

From Dev

AlertDialog crashes 2d onclick (Android/Java)

From Dev

ArrayIndexOutOfBoundException on calling View.draw(Canvas) method

From Dev

ArrayIndexOutOfBoundException Error in simple_list_item_checked

From Dev

JBoss Wildfly 9.0 ArrayIndexOutOfBoundException [asm 3.1]

From Dev

Filtering 2d Array / Returning ArrayIndexOutofBoundException

From Dev

Jackson-Databind ArrayIndexOutOfBoundException Version 2.6.0

Related Related

  1. 1

    Android fragments alertDialog java.lang.IllegalStateException

  2. 2

    JAVA: ArrayIndexOutOFBoundException while reading imported txt

  3. 3

    JAVA: ArrayIndexOutOFBoundException while reading imported txt

  4. 4

    NullPointerException: Attempt to invoke virtual method AlertDialog.setTitle(java.lang.CharSequence) on a null object reference

  5. 5

    Android: custom AlertDialog, viewPager : java.lang.IllegalArgumentException: No view found for id

  6. 6

    ListView with imageView and TextView in AlertDialog leeaks memory and get Java.Lang.OutOfMemoryError

  7. 7

    ArrayIndexOutOfBoundException in HashMap

  8. 8

    Android-Picasso java.lang.IllegalArgumentException: Target must not be null. when the image view is in a custom layout for a alertdialog

  9. 9

    ArrayIndexOutofBoundException - Java - Android - Receiving streaming data and storing in multi-dimensional array Java

  10. 10

    Adding JCombobox in JTable - ArrayIndexOutOfBoundException

  11. 11

    ArrayIndexOutOfBoundException with set number of elements

  12. 12

    Adding JCombobox in JTable - ArrayIndexOutOfBoundException

  13. 13

    program keeps throwing an ArrayIndexOutofBoundException

  14. 14

    ArrayIndexOutOfBoundException vs IndexOutOfBoundException

  15. 15

    Java, Calling an AlertDialog from multiple classes

  16. 16

    java.lang.ClassNotFoundException: A

  17. 17

    "java.lang.NoClassDefFoundError"

  18. 18

    java.lang.NullPointerException?

  19. 19

    java.lang.NoSuchFieldException

  20. 20

    Java.lang.StringIndexOutOfBoundsException

  21. 21

    java.lang.ArrayIndexOutOfBoundsException

  22. 22

    Java - java.lang.NoSuchMethodException

  23. 23

    java.lang.NoClassDefFoundError: groovy/lang/GroovyObject

  24. 24

    AlertDialog crashes 2d onclick (Android/Java)

  25. 25

    ArrayIndexOutOfBoundException on calling View.draw(Canvas) method

  26. 26

    ArrayIndexOutOfBoundException Error in simple_list_item_checked

  27. 27

    JBoss Wildfly 9.0 ArrayIndexOutOfBoundException [asm 3.1]

  28. 28

    Filtering 2d Array / Returning ArrayIndexOutofBoundException

  29. 29

    Jackson-Databind ArrayIndexOutOfBoundException Version 2.6.0

HotTag

Archive