我正在尝试从活动A向活动B发送一个ArrayListobject,但是当我尝试启动活动B时,抛出了一个类异常
这是我的发送和接收语法。问题是 - 如何从object []转换为string []?
或者如果它不可能 - 任何其他解决方案?
ACTIVITY A
Intent intent = new Intent(MainActivity.this, CalculationsActivity.class);
intent.putExtra("strObj2D", stringObjects2D);
startActivity(intent);
活动B
private ArrayList <String[][]> stringObjects2D = new ArrayList <String[][]> () ;
stringObjects2D = (ArrayList<String[][]>) getIntent().getParcelableExtra("strObj2D");
// the size of the arraylist is printed before the crash - so the data seems to be there, that is 3 2D-arrays.
System.out.println("size = " + stringObjects2D.size());
// its here the crash comes, trying to get the second listitem in the arraylist
String[] string = (String[]) stringObjects2D.get(0)[1];
logccat
08-04 22:00:16.290: E/AndroidRuntime(9458): FATAL EXCEPTION: main
08-04 22:00:16.290: E/AndroidRuntime(9458): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.celestialnavigationii/com.example.celestialnavigationii.Calculati onsActivity}: java.lang.ClassCastException: java.lang.Object[] cannot be cast to java.lang.String[][]
08-04 22:00:16.290: E/AndroidRuntime(9458): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2100)
08-04 22:00:16.290: E/AndroidRuntime(9458): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2125)
08-04 22:00:16.290: E/AndroidRuntime(9458): at android.app.ActivityThread.access$600(ActivityThread.java:140)
08-04 22:00:16.290: E/AndroidRuntime(9458): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1227)
08-04 22:00:16.290: E/AndroidRuntime(9458): at android.os.Handler.dispatchMessage(Handler.java:99)
08-04 22:00:16.290: E/AndroidRuntime(9458): at android.os.Looper.loop(Looper.java:137)
08-04 22:00:16.290: E/AndroidRuntime(9458): at android.app.ActivityThread.main(ActivityThread.java:4898)
08-04 22:00:16.290: E/AndroidRuntime(9458): at java.lang.reflect.Method.invokeNative(Native Method)
08-04 22:00:16.290: E/AndroidRuntime(9458): at java.lang.reflect.Method.invoke(Method.java:511)
08-04 22:00:16.290: E/AndroidRuntime(9458): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1006)
08-04 22:00:16.290: E/AndroidRuntime(9458): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773)
08-04 22:00:16.290: E/AndroidRuntime(9458): at dalvik.system.NativeStart.main(Native Method)
08-04 22:00:16.290: E/AndroidRuntime(9458): Caused by: java.lang.ClassCastException: java.lang.Object[] cannot be cast to java.lang.String[][]
08-04 22:00:16.290: E/AndroidRuntime(9458): at com.example.celestialnavigationii.CalculationsActivity.onCreate(CalculationsActivity.java:34)
08-04 22:00:16.290: E/AndroidRuntime(9458): at android.app.Activity.performCreate(Activity.java:5184)
08-04 22:00:16.290: E/AndroidRuntime(9458): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1083)
08-04 22:00:16.290: E/AndroidRuntime(9458): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2064)
08-04 22:00:16.290: E/AndroidRuntime(9458): ... 11 more
编辑:如果我更改为getParcelableExtra,我会得到一个NullPointerException
答案 0 :(得分:0)
使用putStringArrayListExtra方法在Acitvity A上添加额外信息,并使用活动B上的getStringArrayListExtra方法进行检索。
答案 1 :(得分:0)
尝试使用getIntent().getParcelableArrayListExtra("strObj2D")
代替getIntent().getParcelableExtra("strObj2D")
答案 2 :(得分:0)
您需要将ArrayList <String[][]>
作为Serializable
传递。 ArrayList
实施Serializable
,因此您只需使用putExtra(...)
方法。你应该像这样收到它
ArrayList<String[][]> list = (ArrayList<String[][]>) getSerializableExtra("strObj2D");