开发者

android: passing bundle changes type of object

I have stumbled onto a problem with an android app im writing.

Activity 1 looks like this:

// other code here:     
bundle.putString("MyKey" , My_Large_String_containing_HTML);
Log.d(tag + " - TEST" , "tmp CLASS:" + bundle.getString("MyKey").getClass());
Log.d(tag + " - TEST" , "tmp FETCHED:" + bundle.getString("MyKey"));


// make new intent, stuff it with bundle
Intent Activity2Intent = new Intent(this.getApplicationContext(), Activity2.class);

Activity2Intent.putExtras(bundle);
Log.i(tag, "Bundle stuffed.. preparing to make a new screen...");

startActivityForResult(Activity2Intent, 0);

Activity 2 looks like this:

// other set up code
bundle = this.getIntent().getExtras();

My_Large_String_containing_HTML = bundle.getString("MyKey");

/* throws an exception above
*
* WARN/Bundle(976): Key description expected String but value was a [Ljava.lang.String;.  The default value <null> was returned.
*
* WARN/Bundle(976): Attempt to cast generated internal exception:

* WARN/Bundle(976): java.lang.ClassCastException: [Ljava.lang.String;
* WARN/Bundle(976):     at android.os.Bundle.getString(Bundle.java:1开发者_C百科040)

*/

TLDR; I have a string, with HTML (usually) in it. I need it to be available in a second activity. I put it in a bundle; add a bundle to an intent. Fire off the intent, and get the bundle. i TRY to get the string from the bundle, but i get an exception saying that my array cant be gotten.

I never did anything with an array :/

So what's my problem and how do I fix it?


You just put extras straight into the intent:

Activity2Intent.putExtra("MyKey", My_Large_String_containing_HTML);

Then to get the extras, you have to grab the bundle from the intent:

Bundle bundle = this.getIntent().getExtras();
My_Large_String_containing_HTML = bundle.getString("Mykey");


After a few hours of debugging, I finally found out what the problem was.

Let me explain: "MyKey" was a string constant from a 3rd class.

Class3:

static protected final String My_Static_string_key = "description";

Activity2:

My_Large_String_containing_HTML = bundle.getString(Class3.My_Static_string_key);

Activity1:

bundle.putString(Class3.My_Static_string_key , My_Large_String_containing_HTML);

I tried to store and retrieve all sorts of strings with that key; long, short, with HTML, with no HTML...etc. Nothing worked!

Then i changed the key. Instead of using Class3.My_Static_string_key as the key to store and retrieve by,

I hard-coded it to the string stored in Class3 - "description". this still failed.

last ditch attempt to fix: I changed the hard coding of the string to "BOB"

It worked!

I changed the string in my Class3 and it still works.

tldr: it was the key i was using. for some reason, the string i was using broke things. i changed the string in the key and now everything works.


Use like this

 Activity2Intent.putExtra("MyKey" , My_Large_String_containing_HTML);
    startActivity(Activity2Intent);

    //To receive from another Activity

            Bundle bundle = getIntent().getExtras();
            String result=bundle.getString("mykey");
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜