简体   繁体   中英

Android - Passing more variebles via intent makes app crash

So, here is the code from the main activity(the main screen,) if you click on play a random activity( a quiz question) will show up. The problem is that, i am trying to pass an integer, default score to question #1, where the score will be 5(it's the default).

if the user clicks on the wrong button, it will navigate the user to Question #2, and for Question #2, the score will be minus2, less than 2.

int defaultScore = 51;



// start Play Intent
public void onPlay(View view){
    Random r = new Random();
    int XML_random = r.nextInt(5)+1; // 5 different Quiz XML files
    Intent startQuiz = new Intent();
    switch(XML_random){
    case 1:
        startQuiz.setClass(view.getContext(), Quiz_1.class);
    break;
    case 2:
        startQuiz.setClass(view.getContext(), Quiz_2.class);
    break;
    case 3:
        startQuiz.setClass(view.getContext(), Quiz_3.class);
    break;
    case 4:
        startQuiz.setClass(view.getContext(), Quiz_4.class);
    break;
    case 5:
        startQuiz.setClass(view.getContext(), Quiz_5.class);
    break;
    } // end of the Random switch
    startQuiz.putExtra("passScore", defaultScore); 
    startActivity(startQuiz); 

}

So, next if a random activity is chosen, then it's score will be 5, because for the first question, I want the users to start with 5 as a start. So, here is an activty(Question #1) and if the user clicks on the wrong button then Question #2 will show up. And for this activity, the score will be minus 2. Because the user chose the wrong answer.

Question 1 - user clicks on the wrong button:

public class Quiz_1 extends Activity {
TextView textviewScore;
int current_score = 0;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.quiz_1);
    textviewScore = (TextView) findViewById(R.id.q_result);   // declaring the TextView 
    Bundle extras = getIntent().getExtras(); 
    if (extras != null) 
     {
       current_score   = extras.getInt("passScore");
    }

    textviewScore.setText(String.valueOf(current_score));

} // end of onCreate

public void on_quiz_1_wrong(View view){  // button clicked the wrong answer
    current_score = current_score - 2;
    Intent quiz1 = new Intent(this, Quiz_2.class);
    startActivity(quiz1);
    quiz1.putExtra("passNewScore", current_score);

}

And here is Question #2, and for this activity i want the score to be minus 2.

public class Quiz_2 extends Activity {
TextView textviewScore;
int current_score = 0;
int getScore=0;
@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.quiz_2);
    textviewScore = (TextView) findViewById(R.id.q_result);   // declaring the TextView 
    Bundle extras = getIntent().getExtras(); 
    if (extras != null) 
     {
       current_score   = extras.getInt("passScore");
       getScore = extras.getInt("passNewScore");
    }
    current_score = current_score - getScore;
    textviewScore.setText(String.valueOf(current_score));

} // end of onCreate

you must first put extra to your intent then start it.

 quiz1.putExtra("passNewScore", current_score);
 startActivity(quiz1);

You are not passing the value for key "passScore" in quiz1 and calling quiz2, so how can you expect to retrieve the value on "passScore" in quiz2 ?

Pass the value for key "passScore" in quiz1.java in this method on_quiz_1_wrong(View view)

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