我在编写 Android 应用程序时偶然发现了一个问题。
Activity 1 如下所示:
// 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 如下所示:
// 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:1040)
*/
TLDR;我有一个字符串,其中通常包含 HTML。我需要它在第二个 Activity 中可用。我把它 bundle 成一 bundle ;将 bundle 添加到 Intent 中。激发 Intent ,并获得 bundle 。 我尝试从 bundle 中获取字符串,但出现异常,提示无法获取我的数组。
我从来没有对数组做过任何事情:/
那么我的问题是什么以及如何解决它?
最佳答案
您只需将额外内容直接放入 Intent 即可:
Activity2Intent.putExtra("MyKey", My_Large_String_containing_HTML);
然后要获得额外的内容,您必须从 Intent 中获取 bundle :
Bundle bundle = this.getIntent().getExtras();
My_Large_String_containing_HTML = bundle.getString("Mykey");
关于java - android:传递包会改变对象的类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6619384/