getIntent().getStringExtra() shows null

寵の児 提交于 2019-12-22 06:41:08

问题


I m using one EditText field and one spinner. I have to pass teh results of both to the next Activity. here, reqd_bloodgroup is the spinner item, i converted into String using: reqd_bloodgrp = String.valueOf(spinner.getSelectedItem()); inside onItemSelected() of spinner.

intent.putExtra("city", citySelected.getText().toString());
intent.putExtra("bloodgroup", reqd_bloodgrp);
intent = new Intent(FindDonor.this,SpecificDonor.class);
startActivity(intent);

Here when i try to display these, there's no problem. They're correctly displayed. But when i try to fetch them in SpecificDonor activity, they show null values. The code used here is:

String text_city,text_bloodgroup;
text_city = getIntent().getStringExtra("city");
text_bloodgroup = getIntent().getStringExtra("bloodgroup");
Toast.makeText(getApplicationContext(), text_city + " " + "bloodgrp: " + text_bloodgroup, Toast.LENGTH_SHORT).show();

What could be the problem?


回答1:


I think that you must do the:

intent = new Intent(FindDonor.this,SpecificDonor.class);

before adding extras. Try with:

intent = new Intent(FindDonor.this,SpecificDonor.class);
intent.putExtra("city", citySelected.getText().toString());            
intent.putExtra("bloodgroup", reqd_bloodgrp);
startActivity(intent);


来源:https://stackoverflow.com/questions/11078896/getintent-getstringextra-shows-null

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!