How to fix ArrayIndexOutOfBoundsException when a activity is reloaded in Android?

Alex Mamo

I have created programmatically, 5 radio groups with 4 radio buttons each. I set OnClickListener on the reset button that i have created too. I want when someone clicks the button, to restart my activity. How is it even possible? When the app first time starts, it works fine but when i press the button to reload the activity, the emulator crashes. If i comment the lines where i create the radio groups and the radio buttons and i press the button, the activity is reloading fine otherwise i have this error: Unable to start activity ComponentInfo{...}: java.lang.ArrayIndexOutOfBoundsException: length=4; index=4. How can reload the activity without issues? Here is my code:

    answerGroup = new RadioGroup[5];
    answer = new RadioButton[4];
    int i = 0;
    for (Question qn : questions) {
        answerGroup[i] = new RadioGroup(this);
        answerGroup[i].setOrientation(RadioGroup.VERTICAL);
        int j = 0;
        for (Answer an : answers) {
            if (qn.getID() == an.getQuestion_id_answer()) {
                answer[j] = new RadioButton(this);
                answer[j].setText(an.getAnswer());
                answerGroup[i].addView(answer[j]);
                j++;
            }
        }
        linearLayout.addView(answerGroup[i]);
        i++;
    }

    restartButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = getIntent();
            intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
            finish();
            startActivity(intent);
        }
    });

Thanks!

Jack Sierkstra

The activity is probably trying to reference to old view elements (from before the Activity restart). Try to add your view components:

  • RadioGroup
  • RadioButton

As class variables. Can you try that and tell me what it is doing? I will update this answer according to the input you give me.

EDIT:

I assume that you are using a ArrayList<Question> and ArrayList<Answer> as the questions and answers variable.

      for (Question qn : questions) {
            RadioGroup answerGroup = new RadioGroup(this);
            answerGroup.setOrientation(RadioGroup.VERTICAL);
            for (Answer an : qn.getAnswers()) {
                if (qn.getID() == an.getQuestion_id_answer()) {
                    RadioButton answer = new RadioButton(this);
                    answer.setText(an.getAnswer());
                    answerGroup.addView(answer);
                }
            }
            linearLayout.addView(answerGroup);
        }

Maybe you should add a getter method to your question called: getAnswers(); and a variable to your Question model called List<Answer> answers. Ofcourse, these answers need to be set before you try to do anything with them.

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 GCM messages activity - reloaded when ever the activity starts

From Dev

How to fix the ArrayIndexOutOfBoundsException in Adapter android

From Dev

How to fix null pointer exception when using two Volley requests in same activity in Android?

From Dev

How to fix null pointer exception when using two Volley requests in same activity in Android?

From Dev

Image reloaded when scroll listView in android

From Dev

How to keep tab active when page is reloaded?

From Dev

How to fix ArrayIndexOutOfBoundsException for a two dimensional array

From Dev

How do I fix this ArrayIndexOutOfBoundsException in my hashtable?

From Dev

How to fix Tab Activity action bar up color in Android?

From Dev

How to finish Activity when starting other activity in Android?

From Dev

android - how to finish an activity when the orientation is changed?

From Dev

Android: How To Start An Activity When Screen Locked

From Dev

Android: How To Start An Activity When Screen Locked

From Dev

how to do something when an activity finished in android?

From Dev

How to stop a MediaPlayer when changing activity in Android

From Dev

How to confirm when user change activity in android

From Dev

How to remember scroll pos of the `div` to restore it when page is reloaded?

From Dev

How to remember scroll pos of the `div` to restore it when page is reloaded?

From Dev

How to add colours when a page is reloaded using javascript and jQuery

From Dev

justifiedGallery break when reloaded

From Dev

How to Fix the Runtime Error : java.lang.ArrayIndexOutOfBoundsException: 0

From Dev

How to fix java.lang.ArrayIndexOutOfBoundsException: length=1; index=1

From Dev

How to Fix the Runtime Error : java.lang.ArrayIndexOutOfBoundsException: 0

From Dev

How to update listview when back pressed from another activity android?

From Dev

How to not finish activity when the phone call arrived android?

From Dev

How to prevent notification from opening an activity or removed when clicked in Android?

From Dev

How to remove all notifications when an android app (activity or service) is killed?

From Dev

Android:Understanding how does an activity start when application launches

From Dev

android How to open last activity when tapping notification

Related Related

  1. 1

    Android GCM messages activity - reloaded when ever the activity starts

  2. 2

    How to fix the ArrayIndexOutOfBoundsException in Adapter android

  3. 3

    How to fix null pointer exception when using two Volley requests in same activity in Android?

  4. 4

    How to fix null pointer exception when using two Volley requests in same activity in Android?

  5. 5

    Image reloaded when scroll listView in android

  6. 6

    How to keep tab active when page is reloaded?

  7. 7

    How to fix ArrayIndexOutOfBoundsException for a two dimensional array

  8. 8

    How do I fix this ArrayIndexOutOfBoundsException in my hashtable?

  9. 9

    How to fix Tab Activity action bar up color in Android?

  10. 10

    How to finish Activity when starting other activity in Android?

  11. 11

    android - how to finish an activity when the orientation is changed?

  12. 12

    Android: How To Start An Activity When Screen Locked

  13. 13

    Android: How To Start An Activity When Screen Locked

  14. 14

    how to do something when an activity finished in android?

  15. 15

    How to stop a MediaPlayer when changing activity in Android

  16. 16

    How to confirm when user change activity in android

  17. 17

    How to remember scroll pos of the `div` to restore it when page is reloaded?

  18. 18

    How to remember scroll pos of the `div` to restore it when page is reloaded?

  19. 19

    How to add colours when a page is reloaded using javascript and jQuery

  20. 20

    justifiedGallery break when reloaded

  21. 21

    How to Fix the Runtime Error : java.lang.ArrayIndexOutOfBoundsException: 0

  22. 22

    How to fix java.lang.ArrayIndexOutOfBoundsException: length=1; index=1

  23. 23

    How to Fix the Runtime Error : java.lang.ArrayIndexOutOfBoundsException: 0

  24. 24

    How to update listview when back pressed from another activity android?

  25. 25

    How to not finish activity when the phone call arrived android?

  26. 26

    How to prevent notification from opening an activity or removed when clicked in Android?

  27. 27

    How to remove all notifications when an android app (activity or service) is killed?

  28. 28

    Android:Understanding how does an activity start when application launches

  29. 29

    android How to open last activity when tapping notification

HotTag

Archive