问题
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