dylan murphy
dylan murphy

Reputation: 1370

problem filling arrays with for loops

i have a boolean[], checkbox[], and view[] (impactsb[],impactsc[],impactsv[] respectively) that each have a length of 8. i know that that the first position is 0, the next is 1, etc and that the last position is 7. for some reason, my app crashes when you try to do anything with the last position, in other words, 0-6 work fine, 7 never has. what am i doing wrong?

here is my code for this class, which is its own activity an part of a main activity as a tab. sorry about the sloppy formatting.

package com.citsci.mardeb;

... imports ....

public class Impacts extends Activity implements View.OnClickListener
    {
    static boolean[] impactsb = new boolean[] {false, false, false, false, false, false, false, false};

@Override
public void onCreate (Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);     
    setContentView(R.layout.impacts);

  View[] impactsv = new View[]
     {
    findViewById(R.id.gas_oil),
        findViewById(R.id.ghost_fishing),
        findViewById(R.id.marsh_damage),
        findViewById(R.id.nav_haz),
        findViewById(R.id.shell_damage),
        findViewById(R.id.waste_pollution),
        findViewById(R.id.wild_entang),
        findViewById(R.id.other)
        };

  CheckBox[] impactsc = new CheckBox[impactsv.length];

  for (int i = 0; i < 8; i++)
     {
     impactsc[i] = (CheckBox) impactsv[i];
     impactsc[i].setTag(i);
     impactsc[i].setOnClickListener(this);
     }

}// end of onCreate

@Override
public void onClick(View v)
    {
  switch ((Integer)v.getTag())
    {
     case (0):
        impactsb[0] =! impactsb[0];
        break;
     case (1):
        impactsb[1] =! impactsb[1];
        break;
     case (2):
        impactsb[2] =! impactsb[2];
        break;
     case (3):
        impactsb[3] =! impactsb[3];
        break;
     case (4):
        impactsb[4] =! impactsb[4];
        break;
     case (5):
        impactsb[5] =! impactsb[5];
        break;
     case (6):
        impactsb[6] =! impactsb[6];
        break;
     case (7):
        impactsb[7] =! impactsb[7];
        EditText view = (EditText) findViewById(R.id.other);
        if (view.getVisibility() == View.INVISIBLE)
                view.setVisibility(View.VISIBLE);
        else
            view.setVisibility(View.INVISIBLE);
        break;
        }
    }
}// end of Impacts.class

error codes:

ActivityThread.performLaunchActivity(ActivityThread$ActivityClientRecord, Intent) line: 1768    
ActivityThread.handleLaunchActivity(ActivityThread$ActivityClientRecord, Intent) line: 1784 
ActivityThread.access$1500(ActivityThread, ActivityThread$ActivityClientRecord, Intent) line: 123   
ActivityThread$H.handleMessage(Message) line: 939

this is related to this previous question and this other previous question

Upvotes: 0

Views: 119

Answers (2)

Caspar Harmer
Caspar Harmer

Reputation: 8117

Your problems is not indexes or array lengths.
The issue is that you have a TextView called "other" and a CheckBox named "other" and findViewById doesn't know which one to grab when you call it.
Change the TextView id it "other_text" and your problems will be solved.

Upvotes: 3

Robert
Robert

Reputation: 6540

I'm a little unsure about your question, there's a ton of code here and what seems like 2 unrelated questions. As for the array index issue:

|0|1|2|3|4|5|6|7| Length = 8

So length=8, if you do a for loop like:

for (int i=0; i<8; i++)

the values will range from the 8 numbers 0-7 because you used less-than. So just set length to the size you created, and write your for loops like the above.

Upvotes: 1

Related Questions