How to get String from Intent?

南笙酒味 提交于 2019-12-11 23:50:46

问题


I wrote some code to get text from the EditView, put it in the Intent Extras and set it in a TextView on other Activity.

Here is my code from the sending Activity

Intent intent = new Intent(MyLayoutActivity.this, LayoutForOneActivity.class);
intent.putExtra("CaptionOne", String.valueOf(txt_caption1.getText()));
startActivity(intent);

And here is the code from my receiving Activity

Intent intent1 = getIntent();
Bundle bundle = intent1.getExtras();

if (bundle != null) {
    caption = bundle.getString("CaptionOne");
}

If try to run this code I get the ClassCastException looking like this:

java.lang.ClassCastException: android.text.SpannableString cannot be cast 
to java.lang.String

回答1:


use

intent.putExtra("CaptionOne", txt_caption1.getText().toString);

instead of

intent.putExtra("CaptionOne", String.valueOf(txt_caption1.getText()));

and also datatype of caption should be String




回答2:


use

Intent i = getIntent();
String Data = i.getStringExtra("Data");


来源:https://stackoverflow.com/questions/40260154/how-to-get-string-from-intent

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