IndexOutOfBounds应用程序中的错误

时间:2013-02-11 15:45:05

标签: android exception indexoutofboundsexception

我不知道如何解决我得到的错误。我的logcat输出如下所示,正如人们所要求的那样,我还包含了Playing.class的代码

logcat的:

 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){

        }
    }     

   }    

2 个答案:

答案 0 :(得分:-1)

您可以告诉您的类Playing的第449行引发异常,因为从下到上这是堆栈跟踪在Java代码之前的最后一行。

02-11 23:23:41.212: E/AndroidRuntime(2416): at com.ithinkdictionary.Playing.question(Playing.java:449)

答案 1 :(得分:-1)

您在使用此代码时可能会出现问题:

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

它的作用是生成0到数组大小之间的随机数。 但是,如果这返回一个等于ar.size()的整数,尝试访问此索引将导致您遇到的IndexOutOfBoundsException。这是因为数组从0开始索引(即50个元素数组的索引为0到49,所以如果rand.nextInt返回50,则会出现错误)

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

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