-2

我不知道如何解决我遇到的错误。我的 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)

演奏类:

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

        }
    }     

   }    
4

2 回答 2

-1

您可以在类的第 449 行抛出异常,Playing 因为从下往上看,这是堆栈跟踪在 java 代码中之前的最后一行。

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

于 2013-02-11T15:47:47.507 回答
-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);
于 2013-02-11T17:35:20.240 回答