我在从之前的活动中获取值时遇到问题。我正在创建一个使用彩色按钮作为选择的测验应用。基本上,你只需要选择指令所说的按钮。这是代码:
public class EasyMode2Activity extends AppCompatActivity {
Intent g = getIntent(); // get values from EasyMode1Activity.java
public int score = g.getIntExtra("score", 5);
public final int questionTotal = 5;
public int question = g.getIntExtra("questions", 5);
public int questionsAnswered = g.getIntExtra("questionsAnswered", 5);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_easy_mode2);
Button btn4 = (Button) findViewById(R.id.button4);
Button btn5 = (Button) findViewById(R.id.button5);
Button btn6 = (Button) findViewById(R.id.button6);
btn4.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
question++;
TextView scoreText = (TextView) findViewById(R.id.txtScore2);
scoreText.setText(scoreText.getText() + "" +score);
toResults(view);
}
});
btn5.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
question++;
TextView scoreText = (TextView) findViewById(R.id.txtScore2);
scoreText.setText(scoreText.getText() + "" +score);
toResults(view);
}
});
btn6.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
question++;
score++;
TextView scoreText = (TextView) findViewById(R.id.txtScore2);
scoreText.setText(scoreText.getText() + "" +score);
questionsAnswered++;
toResults(view);
}
});
TextView questionTxt = (TextView) findViewById(R.id.txtquestion_number2);
questionTxt.setText(questionTxt.getText() + " " +question+ " of " +questionTotal);
final Chronometer timer = (Chronometer) findViewById(R.id.chronometer2);
timer.start();
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
public void toResults (View view) {
Intent i = new Intent (this, ResultsActivity.class);
Bundle b = new Bundle();
b.putInt("score", score); // store the score
b.putInt("questionsAnswered", questionsAnswered);
b.putInt("question", question);
i.putExtras(b); // put your score to your next intent
startActivity(i);
}
}
另外,我尝试在手机上运行该应用程序。它崩溃了。之前的活动(EasyModeActivity1 - 它们具有类似的代码,但它具有初始值的例外)可以工作但是一旦我点击该活动中的任何按钮,它应该转到EasyModeActivity2但它会崩溃。
答案 0 :(得分:0)
放置这些行:
Intent g = getIntent(); // get values from EasyMode1Activity.java
public int score = g.getIntExtra("score", 5);
public final int questionTotal = 5;
public int question = g.getIntExtra("questions", 5);
public int questionsAnswered = g.getIntExtra("questionsAnswered", 5);
在onCreate里面是这样的:
Intent g = getIntent(); // get values from EasyMode1Activity.java
if (g != null) {
score = g.getIntExtra("score", 5);
questionTotal = 5;
question = g.getIntExtra("questions", 5);
questionsAnswered = g.getIntExtra("questionsAnswered", 5);
}
就在这一行之后:
setContentView(R.layout.activity_easy_mode2);
传球时:
Intent intent = new Intent(this, DestinationActivity.class)
intent.putExtra("myStringKey", "My String value");//pass string
intent.putExtra("myBooleanKey", false);//pass boolean
intent.putExtra("myIntKey", 50);//pass integer
intent.putExtra("score", 50);
intent.putExtra("unanswered", 100);
intent.putExtra("questionsAnswered", 15);
startActivity(intent);