繁体   English   中英

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

[英]Trying to transfer data from one activity to another in android but it show null value on another activity

我正在尝试将数据从一个活动传输到 android 中的另一个活动,但它在另一个活动上显示 null 值。

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

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

在 QuizActivity 中:如果您传递给下一个活动的值仍然是 null,则在传递给下一个活动之前使用日志检查值。

Intent intent = new Intent();
Log.d("score",""+score);
Log.d("incorresct",""+incorrectAnswer);

intent.putString("correctAnswer", score); 
intent.putString("incorrectAnswer", incorrectAnswer); 
startActivity(intent);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM