Search code examples
androidandroid-intent

why getStringExtra doesn't give the proper output?


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)

Solution

  • 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,