Android 应用中的索引自动边界错误

Android 应用中的索引自动边界错误,android,exception,indexoutofboundsexception,Android,Exception,Indexoutofboundsexception,我不知道如何修复我遇到的错误。我的logcat输出如下所示,正如人们所要求的,我还包括了Playing.class 日志: 02-11 23:23:41.212: E/AndroidRuntime(2416): FATAL EXCEPTION: main 02-11 23:23:41.212: E/AndroidRuntime(2416): java.lang.IndexOutOfBoundsException: Invalid index 50, size is 50 02-11 23:

我不知道如何修复我遇到的错误。我的logcat输出如下所示,正如人们所要求的,我还包括了
Playing.class

日志:

 02-11 23:23:41.212: E/AndroidRuntime(2416): FATAL EXCEPTION: main
 02-11 23:23:41.212: E/AndroidRuntime(2416): java.lang.IndexOutOfBoundsException: Invalid index 50, size is 50
 02-11 23:23:41.212: E/AndroidRuntime(2416):    at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:257)
 02-11 23:23:41.212: E/AndroidRuntime(2416):    at java.util.ArrayList.remove(ArrayList.java:406)
 02-11 23:23:41.212: E/AndroidRuntime(2416):    at com.ithinkdictionary.Playing.question(Playing.java:449)
 02-11 23:23:41.212: E/AndroidRuntime(2416):    at com.ithinkdictionary.Playing.access$3(Playing.java:428)
 02-11 23:23:41.212: E/AndroidRuntime(2416):    at com.ithinkdictionary.Playing$13.onClick(Playing.java:1459)
 02-11 23:23:41.212: E/AndroidRuntime(2416):    at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:159)
 02-11 23:23:41.212: E/AndroidRuntime(2416):    at android.os.Handler.dispatchMessage(Handler.java:99)
 02-11 23:23:41.212: E/AndroidRuntime(2416):    at android.os.Looper.loop(Looper.java:123)
 02-11 23:23:41.212: E/AndroidRuntime(2416):    at android.app.ActivityThread.main(ActivityThread.java:3683)
 02-11 23:23:41.212: E/AndroidRuntime(2416):    at java.lang.reflect.Method.invokeNative(Native Method)
 02-11 23:23:41.212: E/AndroidRuntime(2416):    at java.lang.reflect.Method.invoke(Method.java:507)
 02-11 23:23:41.212: E/AndroidRuntime(2416):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
 02-11 23:23:41.212: E/AndroidRuntime(2416):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
 02-11 23:23:41.212: E/AndroidRuntime(2416):    at dalvik.system.NativeStart.main(Native Method)
Playing.class:

    public class Playing  extends Activity   {

     public void onClick(View v) {

         if (level <21){

         twist.startAnimation(anim2);
        firstbutton.startAnimation(anim);
        secondbutton.startAnimation(anim);
        thirdbutton.startAnimation(anim);
        fourthbutton.startAnimation(anim);
        twis.start();
        ArrayList<String> ar= new ArrayList<String>();
        String[] twist = {b,c,d,e};
        Random rand = new Random();

        for (String i : twist) {
            ar.add(i);


        }

        for (int z = 0; z < 4; z++){ 
        int select = rand.nextInt(ar.size());
        String random = ar.get(select);
        ar.remove(select);

        if(z==0){
        firstbutton.setText(random);firstholder = random; }    
         if (z==1){
        secondbutton.setText(random);secondholder = random;    }
         if (z==2){
        thirdbutton.setText(random);thirdholder = random;   }
         if (z==3){
        fourthbutton.setText(random);fourthholder = random;   }}




     }else if( level < 61){

         twist.startAnimation(anim2);
        firstbutton.startAnimation(anim);
        secondbutton.startAnimation(anim);
        thirdbutton.startAnimation(anim);
        fourthbutton.startAnimation(anim);
        fifthbutton.startAnimation(anim);
        sixthbutton.startAnimation(anim);

        twis.start();
        ArrayList<String> ar= new ArrayList<String>();
        String[] twist = {b,c,d,e,f,g};
        Random rand = new Random();

        for (String i : twist) {
          ar.add(i);


      }

        for (int z = 0; z < 6; z++){ 
      int select = rand.nextInt(ar.size());
      String random = ar.get(select);
      ar.remove(select);

      if(z==0){
        firstbutton.setText(random);firstholder = random; }    
       if (z==1){
      secondbutton.setText(random);secondholder = random;    }
       if (z==2){
      thirdbutton.setText(random);thirdholder = random;   }
       if (z==3){
      fourthbutton.setText(random);fourthholder = random;   }
       if (z==4){
       fifthbutton.setText(random);fifthholder = random;   }
       if (z==5){
        sixthbutton.setText(random);sixthholder = random;   }}

    }else if( level < 81){

         twist.startAnimation(anim2);
        firstbutton.startAnimation(anim);
        secondbutton.startAnimation(anim);
        thirdbutton.startAnimation(anim);
        fourthbutton.startAnimation(anim);
        fifthbutton.startAnimation(anim);
        sixthbutton.startAnimation(anim);
        seventhbutton.startAnimation(anim);

        twis.start();
        ArrayList<String> ar= new ArrayList<String>();
        String[] twist = {b,c,d,e,f,g,h};
        Random rand = new Random();

        for (String i : twist) {
         ar.add(i);


     }

        for (int z = 0; z < 7; z++){ 
     int select = rand.nextInt(ar.size());
     String random = ar.get(select);
     ar.remove(select);

     if(z==0){
        firstbutton.setText(random);firstholder = random; }    
      if (z==1){
     secondbutton.setText(random);secondholder = random;    }
      if (z==2){
     thirdbutton.setText(random);thirdholder = random;   }
      if (z==3){
     fourthbutton.setText(random);fourthholder = random;   }
      if (z==4){
      fifthbutton.setText(random);fifthholder = random;   }
      if (z==5){
       sixthbutton.setText(random);sixthholder = random;   }
      if (z==6){
          seventhbutton.setText(random);seventhholder = random;   }}

    }else {

         twist.startAnimation(anim2);
    firstbutton.startAnimation(anim);
    secondbutton.startAnimation(anim);
    thirdbutton.startAnimation(anim);
    fourthbutton.startAnimation(anim);
    fifthbutton.startAnimation(anim);
    sixthbutton.startAnimation(anim);
    seventhbutton.startAnimation(anim);
    eightthbutton.startAnimation(anim);

    twis.start();
    ArrayList<String> ar= new ArrayList<String>();
    String[] twist = {b,c,d,e,f,g,h,i};
    Random rand = new Random();

    for (String i : twist) {
        ar.add(i);


    }

    for (int z = 0; z < 8; z++){ 
    int select = rand.nextInt(ar.size());
    String random = ar.get(select);
    ar.remove(select);

    if(z==0){
    firstbutton.setText(random);firstholder = random; }    
     if (z==1){
    secondbutton.setText(random);secondholder = random;    }
     if (z==2){
    thirdbutton.setText(random);thirdholder = random;   }
     if (z==3){
    fourthbutton.setText(random);fourthholder = random;   }
     if (z==4){
     fifthbutton.setText(random);fifthholder = random;   }
     if (z==5){
      sixthbutton.setText(random);sixthholder = random;   }
     if (z==6){
         seventhbutton.setText(random);seventhholder = random;   }
     if (z==7){
         eightthbutton.setText(random);eightthholder = random;   }}

     } 
     }
  });


 }   


 private void question() {


     InputStreamReader inputStream = new InputStreamReader
       (getResources().openRawResource(R.raw.question1));
       BufferedReader br = new BufferedReader(inputStream);

       ArrayList<Integer> ar= new ArrayList<Integer>();
       int[] number= {1,2,3,4,5,6,7,8,9,10, 
                    11,12,13,14,15,16,17,18,19,20,
                    21,22,23,24,25,26,27,28,29,30,
                    31,32,33,34,35,36,37,38,39,40,
                    41,42,43,44,45,46,47,48,49,50};



                    for (int i : number) {
                            ar.add(i);} 
                        Random r = new Random();
                        int select = r.nextInt(ar.size());
                           int random = ar.get(select);
                        ar.remove(random); 

       String theLine="";
       int lineCtr = 0;

       try {          

       while ((theLine = br.readLine()) != null)   {

       if (lineCtr == select) {    

   StringTokenizer st = new   StringTokenizer(theLine,";");
       while(st.hasMoreTokens())

   {   

       //reading and getting data from the database","
                     a = st.nextToken();
                     b = st.nextToken();
                     c = st.nextToken();
                     d = st.nextToken();
                     e = st.nextToken();
                     f = st.nextToken();

                    //button setText
                    question.setText(a);
                    ca.setText(f);

                    ArrayList<String> as= new ArrayList<String>();
                    String[] twist = {b,c,d,e};
                    Random rand = new Random();

                    for (String i : twist) {
                        as.add(i);


                    }

                    for (int z = 0; z < 4; z++){ 
                    int sel = rand.nextInt(as.size());
                    String randoms = as.get(sel);
                    as.remove(sel);

                    if(z==0){
                    firstbutton.setText(randoms);firstholder = randoms; }    
                     if (z==1){
                    secondbutton.setText(randoms);secondholder = randoms;    }
                     if (z==2){
                    thirdbutton.setText(randoms);thirdholder = randoms;   }
                     if (z==3){
                    fourthbutton.setText(randoms);fourthholder = randoms;   }}
           break;

                }

             }lineCtr++;

       }  


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


private void alertdialog(){


 Context mcontext=this; 

    final AlertDialog.Builder alert = new AlertDialog.Builder(
          mcontext);


    if (level == 1){

  alert.setIcon(R.drawable.gamestart);
  alert.setTitle(" ");
  alert.setPositiveButton("OK",
          new DialogInterface.OnClickListener() {

              @Override
              public void onClick(DialogInterface dialog,
                   int which) {
                     click.start();                                           
                     dialog.cancel();
                     levelshow();


              } 
             });
            }else{

                alert.setIcon(R.drawable.check);
                 alert.setTitle("CORRECT");
                 alert.setPositiveButton("Next",

                         new DialogInterface.OnClickListener() {

                             @Override
                           public void onClick(DialogInterface dialog,
                                  int which) { 
                            click.start();
        qn.setText("Question:"+level);                                        

                                   dialog.cancel();

                                  if (level <= 11){

                                      if(level == 11){
                                           levelshow();}  
                                           else{
                                           timer();
                                           question();}}  

                                  else if(level <=21){ 
                                      if(level == 21){
                                           levelshow();}  
                                           else{

                                               timer();
                                           question2();}}  

                                  else if(level <=31){           
                                      if(level == 31){
                                           levelshow();}  
                                           else{
                                           timer();
                                           question3();}}  

                                  else if(level <=41){ 
                                      if(level == 41){
                                           levelshow();}  
                                           else{
                                           timer();
                                           question4();}}  

                                  else if(level <=51){ 
                                      if(level == 51){
                                           levelshow();}  
                                           else{
                                           timer();
                                           question5();}}  

                                  else if(level <=61){ 
                                      if(level == 61){
                                           levelshow();}  
                                           else{
                                           timer();
                                           question6();}}  

                                  else if(level <=71){ 
                                      if(level == 71){
                                           levelshow();}  
                                           else{
                                           timer();
                                           question7();}}  

                                  else if(level <=81){ 
                                      if(level == 81){
                                           levelshow();}  
                                           else{
                                           timer();
                                           question8();}}  

                                  else if (level <=91){ 
                                      if(level == 91){
                                           levelshow();}  
                                           else{
                                           timer();
                                           question9();}} 

                                  else if (level <=100){ 
                                      if(level == 100){
                                           lo();}  
                                           else{
                                           timer();
                                           question10();}}  


                             } 
                            }); 
            }
        alert.show();
}

private void levelshow(){


     Context mcontext=this; 

        final AlertDialog.Builder alert = new AlertDialog.Builder(
              mcontext);


        if (level == 1){

      alert.setIcon(R.drawable.level);
      alert.setTitle(" ");
      alert.setPositiveButton("Start",
              new DialogInterface.OnClickListener() {

                  @Override
                  public void onClick(DialogInterface dialog,
            int which) {
                        click.start();                                        
                         dialog.cancel();
                         question();
                         timer();
                  } 
                 });
                }else if(level == 11){

                    alert.setIcon(R.drawable.level2);
                  alert.setTitle(" ");
                  alert.setPositiveButton("Start",
                          new DialogInterface.OnClickListener() {

                              @Override
                              public void onClick(DialogInterface dialog,
                                int which) {
                    click.start();                                        
                                     dialog.cancel();
                                    question2();
                                    timer();
                                   navigator.setText("LEVEL 2");

                                 } 
                                }); 
                } if(level == 21){

                    alert.setIcon(R.drawable.level3);
                  alert.setTitle(" ");
                  alert.setPositiveButton("Start",
                          new DialogInterface.OnClickListener() {

                              @Override
                              public void onClick(DialogInterface dialog,
                                   int which) {
                 click.start();                                           
                                     dialog.cancel();
                                    question3();
                                    timer();
                                  navigator.setText("LEVEL 3");   
                                ll1.setVisibility(0);
                                 } 
                                }); 
                } if(level == 31){

                    alert.setIcon(R.drawable.level4);
                  alert.setTitle(" ");
                  alert.setPositiveButton("Start",
                          new DialogInterface.OnClickListener() {

                              @Override
                              public void onClick(DialogInterface dialog,
                                   int which) {
                    click.start();                                        
                                     dialog.cancel();
                                     question4();
                                     timer();
                                   navigator.setText("LEVEL 4");
                                 } 
                                }); 
                }if(level == 41){

                    alert.setIcon(R.drawable.level5);
                  alert.setTitle(" ");
                  alert.setPositiveButton("Start",
                          new DialogInterface.OnClickListener() {

                              @Override
                              public void onClick(DialogInterface dialog,
                                   int which) {
                   click.start();                                         
                                     dialog.cancel();
                                     question5();
                                     timer();
                                   navigator.setText("LEVEL 5");
                                 ll2.setVisibility(0);
                                 } 
                                }); 
                } if(level == 51){

                    alert.setIcon(R.drawable.level6);
                  alert.setTitle(" ");
                  alert.setPositiveButton("Start",
                          new DialogInterface.OnClickListener() {

                              @Override
                              public void onClick(DialogInterface dialog,
                                   int which) {
                   click.start();                                         
                                     dialog.cancel();
                                     question6();
                                     timer();
                                   navigator.setText("LEVEL 6");
                                 } 
                                }); 
                } if(level == 61){

                    alert.setIcon(R.drawable.level7);
                  alert.setTitle(" ");
                  alert.setPositiveButton("Start",
                          new DialogInterface.OnClickListener() {

                              @Override
                              public void onClick(DialogInterface dialog,
                                   int which) {
                 click.start();                                           
                                     dialog.cancel();
                                     question7();
                                     timer();
                                   navigator.setText("LEVEL 7");
                                 ll3.setVisibility(0);
                                 } 
                                }); 
                } if(level == 71){

                    alert.setIcon(R.drawable.level8);
                  alert.setTitle(" ");
                  alert.setPositiveButton("Start",
                          new DialogInterface.OnClickListener() {

                              @Override
                              public void onClick(DialogInterface dialog,
                                   int which) {
                 click.start();                                           
                                     dialog.cancel();
                                     question8();
                                     timer();
                                   navigator.setText("LEVEL 8");
                                 } 
                                }); 
                } if(level == 81){

                    alert.setIcon(R.drawable.level9);
                  alert.setTitle(" ");
                  alert.setPositiveButton("Start",
                          new DialogInterface.OnClickListener() {

                              @Override
                              public void onClick(DialogInterface dialog,
                                   int which) {
                 click.start();                                           
                                     dialog.cancel();
                                     question9();
                                     timer();
                                   navigator.setText("LEVEL 9");
                                 ll4.setVisibility(0);
                              }
                                }); 
                }if(level == 91){

                    alert.setIcon(R.drawable.level10);
                  alert.setTitle(" ");
                  alert.setPositiveButton("Start",
                          new DialogInterface.OnClickListener() {

                              @Override
                              public void onClick(DialogInterface dialog,
                                   int which) {
                   click.start();                                         
                                     dialog.cancel();
                                     question10();
                                     timer();
                                   navigator.setText("LEVEL 10");

                                 } 
                                }); 
                } 

      alert.show();

}             

private void timer(){   

    final CountDownTimer Counter = new CountDownTimer(120000 , 1000) {

    public void onTick(long millisUntilFinished) {
        timer.setText(formatTime( millisUntilFinished));
        scr.setText(""+ (millisUntilFinished*2)/1000);


    }

    public void onFinish() {
        timer.setText("GAME OVER");


    } 
    }; Counter.start();     
    submit.setOnClickListener(new View.OnClickListener() {

     public void onClick(View v) {
        submit.startAnimation(anim2);
         String u = test.getText().toString();
            String p = ca.getText().toString();

            if (u.equals(p)){
             check.start();
                    scoring();
                Counter.cancel();
                level++; 
                alertdialog();



               if (level == 10){


                  }


            }

         else  { 
             wrong.start();
             wrong();


     }


          if (v == submit){
            firstbutton.setEnabled(true);
            secondbutton.setEnabled(true);
            thirdbutton.setEnabled(true);
            fourthbutton.setEnabled(true);
            fifthbutton.setEnabled(true);
            sixthbutton.setEnabled(true);
            seventhbutton.setEnabled(true);
            eightthbutton.setEnabled(true);
            test.setText("");





          }     
     }               

   });
} 

public void lo(){

    AlertDialog.Builder alert = new AlertDialog.Builder(this);

    alert.setTitle("CONGRATULATIONS");
    alert.setMessage("you made in the top 10 highscores");

    // Set an EditText view to get user input 
     final EditText input = new EditText(this);
    alert.setView(input);
    input.setGravity(Gravity.CENTER);
    alert.setNegativeButton("Ok", new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int whichButton) {

        if (input.getText().toString().equals("")){

                 Toast.makeText(Playing.this, "Please input your name!",    

        Toast.LENGTH_SHORT).show();
                  lo();
             }
             else{
                 getdata();
                  String data = input.getText().toString();
                 dataname = data;    
               getdata();

                dialog.cancel();
                finish();

             }        
      }
    });

    alert.show();

}


private void wrong(){
 Context mcontext=this; 

    final AlertDialog.Builder alert = new AlertDialog.Builder(
       mcontext);

  alert.setIcon(R.drawable.error1);
  alert.setTitle("WRONG");
  alert.setPositiveButton("try again",
       new DialogInterface.OnClickListener() {

           @Override
           public void onClick(DialogInterface dialog,
                int which) {
                  click.start();                                      
                  dialog.cancel();

           } 
          });
  alert.show();
 }
private void scoring(){

    fv = Integer.parseInt(scored.getText().toString());
    sv = Integer.parseInt(scr.getText().toString());
    ov = 0;

    ov = fv + sv;
    scored.setText("" + ov);

}
private void giveup(){



         Context mcontext=this; 

            final AlertDialog.Builder alert = new AlertDialog.Builder(
                  mcontext);

          alert.setTitle("Are you sure to give up?");
          alert.setNegativeButton("Cancel",
               new DialogInterface.OnClickListener() {

              @Override
              public void onClick(DialogInterface dialog,
                   int which) {
                  click.start();   
                  dialog.cancel();                                            

              } });
          alert.setPositiveButton("OK",
                  new DialogInterface.OnClickListener() {

                      @Override
                      public void onClick(DialogInterface dialog,
                           int which) {
                                                                                               click.start();                                           
                          dialog.cancel();
                           ch();
                         if (top = true){
                             lo();  
                           }else{
                               finish();
                              }
                      } 
                     });
          alert.show();

            }
    public void getdata(){


        try{
        String name = dataname;
        String tsc = scored.getText().toString();
        int tscore = Integer.parseInt(tsc);

        highscore entry = new highscore(Playing.this);
        entry.open();
        entry.createEntry(name, tscore);
        entry.close();
        }catch(Exception e){

        }
    }    
 public void ch(){

        try{

            String tr = scored.getText().toString();
               checks = Integer.parseInt(tr);

               highscore check = new highscore(Playing.this);
               check.open();
               check.checkt(checks);
               check.close();   


        }catch(Exception e){

        }
    }     

   }    
公共类播放扩展了活动{
公共void onClick(视图v){

如果(level在类
Playing
的第449行抛出异常,则可以判断,因为从下到上这是堆栈跟踪在java代码中之前的最后一行


02-11 23:23:41.212:E/AndroidRuntime(2416):在com.itinkdictionary.Playing.question(Playing.java:449)
您的问题可能会在使用此代码时出现:

int select = rand.nextInt(ar.size());
String random = ar.get(select);
它所做的是生成一个介于0和数组大小之间的随机数。但是,如果返回一个等于ar.size()的整数,尝试访问此索引将导致您遇到的
索引AutofBoundsException
。这是因为数组是从0索引的(即,50个元素的数组的索引为0到49,因此如果rand.nextInt返回50,则会出现错误)

这是一个简单的解决方案:

int select = rand.nextInt(ar.size()-1);
String random = ar.get(select);

向我们展示相关的代码。附加
com.I链接字典.Playing
类的源代码。好的,首先,这只是一个错误。看看logcat,我猜Playing.java的第449行可能是for()语句,您在其中放置了类似
for的内容(i=0;i这是有用的信息,但不是答案-这应该列为一个评论,因为它不能解决问题,只是帮助诊断。作为一个拥有8000名代表的人,你应该知道我更喜欢一个好答案,很抱歉你不同意。尽量不要让它破坏你的一天。我的一天很好,谢谢你的提问=)但是回答的全部目的是解决问题。你没有,你在帮助诊断。保存“答案”以便最终被标记为一个-你是正确的,将在
Playing.class
的第449行。但是在我们看到代码之前,不可能回答@Manny的问题(尽管正如我在评论中所说,我有一个很好的怀疑)为什么使用字符串?我需要将select转换为字符串吗?老兄…为什么使用向下投票?您使用了字符串!我只是编辑您的代码以使其工作,我没有质疑实现!更重要的是,我仍然在这里猜测,因为您没有在代码中输入行号,也没有突出显示您添加的代码上的第449行