android - 如何在 Activity 之间传递 double 的二维数组?

标签 android arrays android-activity

我需要在 Activity 之间传递一个 double 的二维数组,这是我到目前为止写的: 在第一个 Activity 中(发件人):

Intent intent =new Intent(MapsActivity.this, RoutePath.class);
Bundle b = new Bundle();
b.putSerializable("other_locations", other_locations);
intent.putExtras(b);
startActivity(intent);

在第二个(接收者)中:

Intent intent = getIntent();
Bundle b = intent.getExtras();
double[][] other_locations=(double[][]) b.getSerializable("other_locations");

但是我得到了 ClassCastException,我是不是做错了什么?

最佳答案

双二维数组转换为字符串二维数组并像您一样发送它:

发送:

b.putSerializable("array", other_locations_string);

得到:

String[][] value = (String[][]) b.getSerializable("array");

然后再次将其转换为双二维数组

此行为的原因是因为 Java(以及 Android)不允许您将对象转换为基本类型或基本类型数组。为什么?因为 Java 考虑了一个有效的强制转换,它将父类(super class)对象转换为子类对象,反之亦然。 String[][] 扩展了 Object,而 double 和 double[][] 没有。

关于android - 如何在 Activity 之间传递 double 的二维数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15884112/

相关文章:

python - 修改矩阵。 python 非常有趣的任务

iphone - 从 NSMutableArray 获取特定值

java - 单击按钮时如何打开新 Activity ?

android - 从我的自定义主屏幕启动时相机和 map 崩溃

android - 单击菜单项返回主 Activity

java - SQLite 数据库 : Insert only if the value does not exist (not via raw SQL command)

android - 错误 :Failed to resolve: com. google.firebase :firebase-core:11. 0.0

android - Android 中同一应用程序的多个通知不起作用?

android - JobSchedulerService 中的 RoomDB

arrays - AWK:强制/转换未初始化变量为类型 "array"而不是 "numeric-string"