why getStringExtra doesn't give the proper out

2019-08-03 13:21发布

问题:

I'm was trying to pass some String from one intent to another. but adt says:

Key text expected String but value was a android.text.SpannableString. The default value was returned.

but I'm using a String as key not what it claims!

here's the code for my first activity:

private int CONTACTS_ACTIVITY_REQUEST = 1001;
public static final String TEXT_KEY = "text";
...

Intent intent = new Intent(this, ContactsActivity.class);
intent.putExtra(TEXT_KEY, text.getText());
startActivityForResult(intent, CONTACTS_ACTIVITY_REQUEST);

my second activity:

Intent intent = this.getIntent();
text = intent.getStringExtra(MainActivity.TEXT_KEY);

thanks.

p.s. here's the full stack trace:

03-22 14:51:32.975: W/Bundle(1248): Key text expected String but value was a android.text.SpannableString.  The default value <null> was returned. 
03-22 14:51:33.006: W/Bundle(1248): Attempt to cast generated internal exception: 
03-22 14:51:33.006: W/Bundle(1248): java.lang.ClassCastException: android.text.SpannableString cannot be cast to java.lang.String 
03-22 14:51:33.006: W/Bundle(1248):     at android.os.Bundle.getString(Bundle.java:1085) 03-22 14:51:33.006: W/Bundle(1248):    at android.content.Intent.getStringExtra(Intent.java:4473) 
03-22 14:51:33.006: W/Bundle(1248):     at com.saeedFri.groupsms.ContactsActivity.onCreate(ContactsActivity.java:39) 
03-22 14:51:33.006: W/Bundle(1248):     at android.app.Activity.performCreate(Activity.java:5133) 
03-22 14:51:33.006: W/Bundle(1248):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087) 
03-22 14:51:33.006: W/Bundle(1248):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175) 
03-22 14:51:33.006: W/Bundle(1248):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261) 
03-22 14:51:33.006: W/Bundle(1248):     at android.app.ActivityThread.access$600(ActivityThread.java:141) 
03-22 14:51:33.006: W/Bundle(1248):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256) 
03-22 14:51:33.006: W/Bundle(1248):     at android.os.Handler.dispatchMessage(Handler.java:99) 
03-22 14:51:33.006: W/Bundle(1248):     at android.os.Looper.loop(Looper.java:137) 
03-22 14:51:33.006: W/Bundle(1248):     at android.app.ActivityThread.main(ActivityThread.java:5103) 
03-22 14:51:33.006: W/Bundle(1248):     at java.lang.reflect.Method.invokeNative(Native Method) 
03-22 14:51:33.006: W/Bundle(1248):     at java.lang.reflect.Method.invoke(Method.java:525) 
03-22 14:51:33.006: W/Bundle(1248):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737) 
03-22 14:51:33.006: W/Bundle(1248):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) 
03-22 14:51:33.006: W/Bundle(1248):     at dalvik.system.NativeStart.main(Native Method)

回答1:

change

intent.putExtra(TEXT_KEY, text.getText());

to

intent.putExtra(TEXT_KEY, text.getText().toString());

in first activity you need send your value, getText method return Editable, so if you want value you need use toString() method.

you can handle that on second class to with

text = intent.getStringExtra(MainActivity.TEXT_KEY).toString();

you need use one of this two way,



回答2:

The value you are getting using getStringExtra() method is SpannableString but you are trying to put it into a String that's why its throwing error as below...

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

You can try using toString() method when you are retrieving the string extra using getStringExtra() method as follows...

Intent intent = this.getIntent();
text = intent.getStringExtra(MainActivity.TEXT_KEY).toString();


回答3:

Got very intresting hack in my case. I did everything as required but i missed it by intializing binding with empty/null string. i had ...

public static final String TITLE = "";

insted of

public static final String TITLE = "Something Here"; // Solution

Folled by your intent later on :-

         String title1 = title.getText().toString();
         Intent data = this.getIntent();
         data.putExtra(TITLE, title1);

Then in receiving class -

  String TITL = data.getStringExtra(SendingClass.TITLE);

Chears !