如何从Intent中获取String? [英] How to get String from Intent?
本文介绍了如何从Intent中获取String?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我写了一些代码从EditView中获取文本,将其放在Intent Extras中,然后在其他Activity的TextView中进行设置。
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.
这是我发送活动的代码
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");
}
如果尝试运行此代码,我将得到如下所示的ClassCastException:
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
推荐答案
使用
intent.putExtra("CaptionOne", txt_caption1.getText().toString);
而不是
intent.putExtra("CaptionOne", String.valueOf(txt_caption1.getText()));
以及标题
的数据类型也应为字符串
and also datatype of caption
should be String
这篇关于如何从Intent中获取String?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文