Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/221.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
试图在android中将数据从一个活动传输到另一个活动,但在另一个活动上显示空值_Android_Android Intent - Fatal编程技术网

试图在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

我试图在android中将数据从一个活动传输到另一个活动,但在另一个活动上显示空值

我在第一次活动中的意图代码

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

我们如何知道
分数
不为空?