试图在android中将数据从一个活动传输到另一个活动,但在另一个活动上显示空值
我试图在android中将数据从一个活动传输到另一个活动,但在另一个活动上显示空值 我在第一次活动中的意图代码试图在android中将数据从一个活动传输到另一个活动,但在另一个活动上显示空值,android,android-intent,Android,Android Intent,我试图在android中将数据从一个活动传输到另一个活动,但在另一个活动上显示空值 我在第一次活动中的意图代码 Intent intent = new Intent(QuizActivity.this, WinnerActivity.class); intent.putExtra("correctAnswer", score); intent.putExtra("incorrectAnswer", inc
Intent intent = new Intent(QuizActivity.this, WinnerActivity.class);
intent.putExtra("correctAnswer", score);
intent.putExtra("incorrectAnswer", incorrectAnswer);
startActivity(intent);
finish();
然后在第二个活动中,我检索了如下论点:
correctAnswers = getIntent().getExtras().getString("correctAnswer");
incorrectAnswers = getIntent().getExtras().getString("incorrectAnswer");
Toast.makeText(this, "Correct answer is " + "\n" + correctAnswers +
"\n" + "Incorrect answers is " + incorrectAnswers, Toast.LENGTH_SHORT).show();
但是toast打印两个变量的值都为null,我不知道为什么。结果活动中的值为null,因为您没有在intent.getExtras()中设置值 改为使用此方法获取WinnerActivity中的值:
correctAnswers = getIntent().getStringExtra("correctAnswer");
incorrectAnswers = getIntent().getStringExtra("incorrectAnswer");
在QuizaActivity中:
如果要传递给下一个活动的值仍然为空,请在传递给下一个活动之前使用日志检查这些值
Intent intent = new Intent();
Log.d("score",""+score);
Log.d("incorresct",""+incorrectAnswer);
intent.putString("correctAnswer", score);
intent.putString("incorrectAnswer", incorrectAnswer);
startActivity(intent);
我们如何知道
分数不为空?