简体   繁体   中英

Android app crashing whilst using intent

I am creating a quiz which loops through a series of images and then goes to a final page. My problem is that the quiz crashes at the end of the images. This is the code:

public class MyActivity extends Activity implements View.OnClickListener
{
static Button next;
static ImageView mainpic;
static RadioGroup radioGroup;
static RadioButton option1;
static RadioButton option2;
static RadioButton option3;
static int[] mapPics = new int[]{R.drawable.ic_launcher, R.drawable.images};
static String [] answers= new String[]{"Spain", "Poland", "Sweden",
       "America", "England", "Austrailia"};
// static int [] correctAnswer= new int[]{2, 1};
static int i=0;
static int a=0;
static int b=1;
static int c=2;
static EditText mTextField;

 @Override
 public void onCreate(Bundle savedInstanceState)
 {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);
  next= (Button)findViewById(R.id.button);
  mainpic= (ImageView)findViewById(R.id.imageView);
  mainpic.setImageResource(mapPics[i]);
  next.setOnClickListener(this);
  addListenerRadioGroup();
  mTextField= (EditText)findViewById(R.id.editText);
  option1.setText(String.valueOf(answers[a]));
  option2.setText(String.valueOf(answers[b]));
  option3.setText(String.valueOf(answers[c]));


}



 public void addListenerRadioGroup(){

  radioGroup = (RadioGroup)findViewById(R.id.radioGroup);
  radioGroup.setOnClickListener(new View.OnClickListener()
  {
     @Override
     public void onClick(View v)
     {
        radioGroup.getCheckedRadioButtonId();
     }
  });

}

 public void setIntent (){
  Intent myIntent = new Intent(this, scores.class);
  startActivity(myIntent);
}

@Override
public void onClick(View v)
{
  i++;
  a=a+3;
  b=b+3;
  c=c+3;

  if(i >=2) {
     setIntent();
  }
  mainpic.setImageResource(mapPics[i]);
  option1.setText(String.valueOf(answers[a]));
  option2.setText(String.valueOf(answers[b]));
  option3.setText(String.valueOf(answers[c]));
  }
}

Where I have if(i >=2) {set intent}, I can set the int i=0 and the images will continuously loop (which I don't want but makes me think its not a problem with the if statement), however with the intent it just wont go to the next class. The error in the logcat is:

05-05 13:55:28.781    2025-2025/com.example.attempt E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.attempt, PID: 2025
java.lang.ArrayIndexOutOfBoundsException: length=3; index=3
        at com.example.attempt.screen2.onClick(screen2.java:80)
        at android.view.View.performClick(View.java:4438)
        at android.view.View$PerformClick.run(View.java:18422)
        at android.os.Handler.handleCallback(Handler.java:733)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:136)
        at android.app.ActivityThread.main(ActivityThread.java:5017)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:515)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
        at dalvik.system.NativeStart.main(Native Method)

The error shows that you are trying to access the index 3 on an array that only has 3 items (index 0, 1, and 2).

I think the problem is in your onClick()

if(i >=2) {
     setIntent();
  }
  mainpic.setImageResource(mapPics[i]);

Let's say i==3, you will set your intent, exit the if and execute

mainpic.setImageResource(mapPics[3]);

I think you should check your mapPics[] array to make sure you have more than 3 items.

Or try that code:

@Override
public void onClick(View v)
{
  i++;
  a=a+3;
  b=b+3;
  c=c+3;

  if(i >=2) {
     setIntent();
  } else{
  mainpic.setImageResource(mapPics[i]);
  option1.setText(String.valueOf(answers[a]));
  option2.setText(String.valueOf(answers[b]));
  option3.setText(String.valueOf(answers[c]));
}
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM