将2D字符串数组传递给另一个Activity - ClassCastException

时间:2014-08-05 13:04:25

标签: android android-intent android-activity classcastexception

我正在尝试将2D-stringArray传递给另一个Activity,但接收器Activity中的方法返回ClassCastException 将信息发送到其他活动

 String[][] my2Dlist = {{"1", "john"}, {"2", "marie"}, {"3", "thomas"}};
 Intent intent = new Intent(MainActivity.this, CalculationsActivity.class);
 Bundle b=new Bundle();
 b.putSerializable("list", my2Dlist);
 intent.putExtras(b);
 startActivity(intent);

然后在接收器活动中

 String[][] myString = (String[][]) getIntent().getExtras().getSerializable("list");

错误:

 Java.lang ClassCastException. Object[] cannot be cast to a String[][].

我不明白 - 当我发送一个String [] []时,如何在其他Activity中成为Object []?

我该怎么做才能解决这个问题?

我看过这篇文章,似乎对他们有用,但不是我的情况

Pass 2D array to another Activity

logcat的

08-05 15:17:48.725: E/AndroidRuntime(15576): FATAL EXCEPTION: main
08-05 15:17:48.725: E/AndroidRuntime(15576): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.celestialnavigationii/com.example.celestialnavigationii.CalculationsActivity}: java.lang.ClassCastException: java.lang.Object[] cannot be cast to java.lang.String[][]
08-05 15:17:48.725: E/AndroidRuntime(15576):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2100)
08-05 15:17:48.725: E/AndroidRuntime(15576):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2125)
08-05 15:17:48.725: E/AndroidRuntime(15576):    at android.app.ActivityThread.access$600(ActivityThread.java:140)
08-05 15:17:48.725: E/AndroidRuntime(15576):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1227)
08-05 15:17:48.725: E/AndroidRuntime(15576):    at android.os.Handler.dispatchMessage(Handler.java:99)
08-05 15:17:48.725: E/AndroidRuntime(15576):    at android.os.Looper.loop(Looper.java:137)
08-05 15:17:48.725: E/AndroidRuntime(15576):    at android.app.ActivityThread.main(ActivityThread.java:4898)
08-05 15:17:48.725: E/AndroidRuntime(15576):    at java.lang.reflect.Method.invokeNative(Native Method)
08-05 15:17:48.725: E/AndroidRuntime(15576):    at java.lang.reflect.Method.invoke(Method.java:511)
08-05 15:17:48.725: E/AndroidRuntime(15576):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1006)
08-05 15:17:48.725: E/AndroidRuntime(15576):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773)
08-05 15:17:48.725: E/AndroidRuntime(15576):    at dalvik.system.NativeStart.main(Native Method)
08-05 15:17:48.725: E/AndroidRuntime(15576): Caused by: java.lang.ClassCastException: java.lang.Object[] cannot be cast to java.lang.String[][]
08-05 15:17:48.725: E/AndroidRuntime(15576):    at com.example.celestialnavigationii.CalculationsActivity.onCreate(CalculationsActivity.java:38)
08-05 15:17:48.725: E/AndroidRuntime(15576):    at android.app.Activity.performCreate(Activity.java:5184)
08-05 15:17:48.725: E/AndroidRuntime(15576):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1083)
08-05 15:17:48.725: E/AndroidRuntime(15576):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2064)
08-05 15:17:48.725: E/AndroidRuntime(15576):    ... 11 more

0 个答案:

没有答案