I am trying to transfer data from one activity to another activity in android but it show null value on another activity.
My Intent Code at first activity
Intent intent = new Intent(QuizActivity.this, WinnerActivity.class);
intent.putExtra("correctAnswer", score);
intent.putExtra("incorrectAnswer", incorrectAnswer);
startActivity(intent);
finish();
Then in second activity i retrive argument like this:
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();
But toast is print both variables value null i don't know why.
The values are null in the resultant activity because you are not setting the values in intent.getExtras ().
Use this method instead to get the values in WinnerActivity:
correctAnswers = getIntent().getStringExtra("correctAnswer");
incorrectAnswers = getIntent().getStringExtra("incorrectAnswer");
In QuizActivity: If the values you are passing on to the next activity is still null check the values using log before passing to the next activity.
Intent intent = new Intent();
Log.d("score",""+score);
Log.d("incorresct",""+incorrectAnswer);
intent.putString("correctAnswer", score);
intent.putString("incorrectAnswer", incorrectAnswer);
startActivity(intent);
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.