Skip to content Skip to sidebar Skip to footer

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 return

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

Solution 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...

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

Solution 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 ...

publicstaticfinalStringTITLE="";

insted of

publicstaticfinalStringTITLE="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 -

StringTITL= data.getStringExtra(SendingClass.TITLE);

Chears !

Post a Comment for "Why Getstringextra Doesn't Give The Proper Output?"