0

所以,这里是来自主活动(主屏幕)的代码,如果你点击播放随机活动(一个测验问题)就会出现。问题是,我试图将一个整数默认分数传递给问题#1,其中分数将为 5(这是默认值)。

如果用户点击了错误的按钮,它会将用户导航到问题#2,而对于问题#2,分数将为负2,小于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); 

}

所以,接下来如果选择一个随机活动,那么它的分数将为 5,因为对于第一个问题,我希望用户从 5 作为开始。所以,这里是一个活动(问题#1),如果用户点击了错误的按钮,那么问题#2 就会出现。而对于这个活动,分数将是负2。因为用户选择了错误的答案。

问题 1 - 用户点击了错误的按钮:

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

}

这是问题 #2,对于这项活动,我希望分数为负 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
4

2 回答 2

1

你必须先把额外的东西放在你的意图上,然后再开始它。

 quiz1.putExtra("passNewScore", current_score);
 startActivity(quiz1);
于 2013-11-03T08:24:39.747 回答
0

您没有在 quiz1 中传递键“passScore”的值并调用 quiz2,那么您如何期望在 quiz2 中检索“passScore”的值?

在此方法中传递 quiz1.java 中键“passScore”的值on_quiz_1_wrong(View view)

于 2013-11-03T09:59:32.910 回答