java.lang.IndexOutOfBoundsException的原因是什么:索引0无效,代码中的大小为0错误?

时间:2016-10-17 13:31:10

标签: android arrays android-studio

我的代码中出现此错误:

 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.nooshindroid.yastashir/com.nooshindroid.yastashir.MainActivity}: java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0  

在dess3和dess 4中出现了这个错误! 我不知道为什么。但是dess3和dess 4的大小是0!

此代码的路由类位于:

    public class Route  extends Activity {
String diff;
Context context;
ArrayList<String> sources, destinations,   destinations2,destinations3,destinations4, answer;
public static ArrayList<Models> routeItems;
public Route(Context context) {
    this.context = context;
}


  public Route(String part) {

    XmlPullParserFactory pullParserFactory;
    try {
        pullParserFactory = XmlPullParserFactory.newInstance();
        XmlPullParser parser = pullParserFactory.newPullParser();

        Bundle bundle = getIntent().getExtras();
        diff = bundle.getString("level");


        InputStream in_s = this.context.getAssets().open(diff);
        parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, false);
        parser.setInput(in_s, null);

        parseXML(parser);

    }
    catch (XmlPullParserException e) {

        e.printStackTrace();
    }
    catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}


public void parseXML(XmlPullParser parser) throws XmlPullParserException, IOException
{

    int eventType = parser.getEventType();
    Models currentRout = null;

    sources = new ArrayList<String>();
    sources.clear();

    destinations = new ArrayList<String>();
    destinations.clear();

    destinations2 = new ArrayList<String>();
    destinations2.clear();

    destinations3 = new ArrayList<String>();
    destinations3.clear();

    destinations4 = new ArrayList<String>();
    destinations4.clear();

    answer = new ArrayList<String>();
    answer.clear();



    while (eventType != XmlPullParser.END_DOCUMENT) {
        String name = null;
        switch (eventType) {
            case XmlPullParser.START_DOCUMENT:
                routeItems = new ArrayList<Models>();
                break;
            case XmlPullParser.START_TAG:
                name = parser.getName();
                if (name.equalsIgnoreCase("route")) {
                    currentRout = new Models();
                } else if (currentRout != null) {
                    if (name.equalsIgnoreCase("source")) {
                        currentRout.source = parser.nextText();
                        if ( !sources.contains(currentRout.source))
                            sources.add(currentRout.source);
                    } else if (name.equalsIgnoreCase("destination")) {
                        currentRout.destination = parser.nextText();
                        try {
                            //if ( !destinations.contains(currentRout.destination))
                            destinations.add(currentRout.destination);
                        }
                        catch (Exception ex)
                        {

                        }
                    } else if (name.equalsIgnoreCase("destination2")) {
                        currentRout.destination2 = parser.nextText();
                        try {

                            //if ( !destinations2.contains(currentRout.destination2))
                            destinations2.add(currentRout.destination2);
                        }
                        catch (Exception ex)
                        {

                        }


                    } else if (name.equalsIgnoreCase("destination2")) {
                        currentRout.destination3 = parser.nextText();
                        try {
                            //if ( !destinations2.contains(currentRout.destination2))
                            destinations3.add(currentRout.destination3);
                        }
                        catch (Exception ex)
                        {

                        }

                    } else if (name.equalsIgnoreCase("destination2")) {
                        currentRout.destination4 = parser.nextText();
                        try {
                            //if ( !destinations2.contains(currentRout.destination2))
                            destinations4.add(currentRout.destination4);
                        }
                        catch (Exception ex)
                        {

                        }

                    } else if (name.equalsIgnoreCase("answer")) {
                        currentRout.answer = parser.nextText();
                        try {
                            //if ( !destinations2.contains(currentRout.destination2))
                            answer.add(currentRout.answer);
                        }
                        catch (Exception ex)
                        {

                        }
                    }
                }
                break;
            case XmlPullParser.END_TAG:
                name = parser.getName();
                if (name.equalsIgnoreCase("route") && currentRout != null) {
                    routeItems.add(currentRout);
                }
        }

        eventType = parser.next();
    }
}
}

我的代码的主要活动如下:

public class MainActivity extends Activity {


public int nextque;


ArrayList<String> sourcess;
ArrayList<String> dess;
ArrayList<String> dess2;
ArrayList<String> dess3;
ArrayList<String> dess4;
ArrayList<String> answer;


int randomInt;
Random random;





Button buttons[] = new Button[8];
String j[] = new String[3];
int sumscore1 = 0;
int sumscore2 = 0;
String diff;
ProgressBar mProgressBar, mProgressBar1;
int edtTimerValue;
TextView textViewShowTime;
CountDownTimer countDownTimer;
long totalTimeCountInMilliseconds;
public void nextque() {
    if ((nextque) + 1 <  sourcess.size()) {
        nextque++;

    }
}
private void setTimer() {
    int time = 0;
    time = edtTimerValue;
    totalTimeCountInMilliseconds = time * 1000;
    mProgressBar1.setMax(time * 1000);
}
public void startTimer() {
    countDownTimer = new CountDownTimer(totalTimeCountInMilliseconds, 1) {
        @Override
        public void onTick(long leftTimeInMilliseconds) {
            long seconds = leftTimeInMilliseconds / 1000;
            mProgressBar1.setProgress((int) (leftTimeInMilliseconds));


            textViewShowTime.setText(String.format("%02d", seconds % 60));
        }

        @Override
        public void onFinish() {
            buttons[7].setBackgroundResource(R.drawable.blank_button);
            textViewShowTime.setText("10");
            textViewShowTime.setVisibility(View.VISIBLE);
            mProgressBar.setVisibility(View.VISIBLE);
            mProgressBar1.setVisibility(View.GONE);
            nextStep();

        }
    }.start();
}

public void nextStep() {


    textViewShowTime.setVisibility(View.VISIBLE);
    equation1.setVisibility(View.VISIBLE);
    equation2.setVisibility(View.VISIBLE);
    setTimer();
    mProgressBar.setVisibility(View.INVISIBLE);
    startTimer();
    mProgressBar1.setVisibility(View.VISIBLE);
    if(sumscore1==100){
        sumscore1=0;
        sumscore2=0;
        score1.setText(String.valueOf(sumscore1));
        score2.setText(String.valueOf(sumscore2));
    }
    if (sumscore2==100){
        sumscore1=0;
        sumscore2=0;
        score1.setText(String.valueOf(sumscore1));
        score2.setText(String.valueOf(sumscore2));
    }
    random = new Random();
    randomInt = random.nextInt(sourcess.size());
    equation1.setText(sourcess.get(nextque));
    equation2.setText(sourcess.get(nextque));

    Random rn = new Random();
    int index = rn.nextInt(3) + 0;
    Log.e("rand poduct", String.valueOf(index));
    nextque();
    equation1 = (TextView) findViewById(R.id.equation1);
    buttons[index].setText( answer.get(nextque));
    dess.size();
    int k = 0;
    for (int i = 0; i <= 3; i++) {
        Log.e("prod", "loop " + i);
        if (i != index ) {
            if(!(answer.get(nextque).equals(j[k]))){
                buttons[i].setText((j[k]));
            }
            k++;
        }
    }
    equation2 = (TextView) findViewById(R.id.equation2);
    Random rt = new Random();
    int index2 = rt.nextInt(3) + 0;
    int index3 = index2 + 4;
    buttons[index3].setText( answer.get(nextque));
    int d = 0;
    for (int i = 4; i <= 7; i++) {
        if (i != index3) {
            if(!(answer.get(nextque).equals(j[d]))){
                buttons[i].setText((j[d]));
            }
            d++;
        }
    }
}
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.activity_main);
    equal1 = (TextView) findViewById(R.id.equal1);
    equal2 = (TextView) findViewById(R.id.equal2);
    bord1 = (TextView) findViewById(R.id.bord1);
    bord2 = (TextView) findViewById(R.id.bord2);
    bakht1 = (TextView) findViewById(R.id.bakht1);
    bakht2 = (TextView) findViewById(R.id.bakht2);
    refreash = (ImageView) findViewById(R.id.refreash);
    textViewShowTime = (TextView) findViewById(R.id.textView_timerview_time);
    edtTimerValue = 10;
    mProgressBar = (ProgressBar) findViewById(R.id.progressbar_timerview);
    mProgressBar1 = (ProgressBar) findViewById(R.id.progressbar1_timerview);
    Bundle bundle = getIntent().getExtras();
    Typeface font = Typeface.createFromAsset(getAssets(), "BYekan.ttf");
    diff = bundle.getString("level");
    textViewShowTime.setTypeface(font);
    route = new Route(getApplicationContext());
    XmlPullParserFactory pullParserFactory;
    try {
        pullParserFactory = XmlPullParserFactory.newInstance();
        XmlPullParser parser = pullParserFactory.newPullParser();

        InputStream in_s = getApplicationContext().getAssets().open("easys.xml");
        parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, false);
        parser.setInput(in_s, null);

        route.parseXML(parser);

    } catch (XmlPullParserException e) {

        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    sourcess = new ArrayList<String>();
    for (int i = 0; i < route.sources.size(); i++) {
        sourcess.add(i, route.sources.get(i));
    }
    dess = new ArrayList<String>();
    for (int i = 0; i < route.destinations.size(); i++) {
        dess.add(i, route.destinations.get(i));
    }
    dess2 = new ArrayList<String>();
    for (int i = 0; i < route.destinations2.size(); i++) {
        dess2.add(i, route.destinations2.get(i));
    }
    dess3 = new ArrayList<String>();
    for (int i = 0; i < route.destinations3.size(); i++) {
        dess3.add(i, route.destinations3.get(i));
    }
    dess4 = new ArrayList<String>();
    for (int i = 0; i < route.destinations4.size(); i++) {
        dess4.add(i, route.destinations4.get(i));
    }
    answer = new ArrayList<String>();
    for (int i = 0; i < route.answer.size(); i++) {
        answer.add(i, route.answer.get(i));

    }
    nextStep();
}
}

任何人都可以帮我解决吗?非常感谢你的关注。

1 个答案:

答案 0 :(得分:0)

数组j的数组大小为3.但是当你在循环中使用它时,你正在迭代它的大小。尝试增加数组的大小并检查。

除错误外,您还会获得错误的行号,如果可能,请将其发布。