将 Serializable 数组从一个活动传递到另一个活动时出现类 Cast 异常 [英] Class Cast Exception when passing array of Serializables from one activity to another

查看:20
本文介绍了将 Serializable 数组从一个活动传递到另一个活动时出现类 Cast 异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含可序列化对象的数组,我正在尝试使用 Intent 的 putExtra() 和 Bundle 的 getSerializable().但是,我收到了一个类转换异常,不明白为什么.

I have an array containing serializable objects and am trying to use Intent's putExtra() and Bundle's getSerializable(). However, I am getting a class cast exception and don't understand why.

下面是代码.UserInfo 是我自己的类,它实现了 Serializable,我已经能够在活动之间传递单独的 UserInfo 对象.我只在尝试使用数组时遇到过这个问题.

Below is the code. UserInfo is my own class which implements Serializable, and I have been able to pass individual UserInfo objects between activities. I've only ran into this problem when attempting to use an array.

发送可序列化的代码:

Intent intent = new Intent( JOIN_GROUP ); //JOIN_GROUP is a constant string

String user_ids[] = packet.userIDs();

int length = user_ids.length;
UserInfo users[] = new UserInfo[length];

for ( int i = 0; i < length; ++i )
    users[i] = getUserByID( user_ids[i] );

intent.putExtra( USERS_IN_GROUP, users );

检索可序列化的代码:

Bundle extra = intent.getExtras();          
String action = intent.getAction();

if ( action.equals(IMService.JOIN_GROUP) )
{   
    //CLASS CAST EXCEPTION
    UserInfo users[] = (UserInfo[]) extra.getSerializable( IMService.USERS_IN_GROUP ); 
}

这里是错误:

问题

我知道我可能只是使用不同的数据结构,但我想了解为什么数组不起作用,因为数组是可序列化的?

I'm aware I could probably just use a different data structure, but I would like to understand why the array does not work since arrays are serializable?

SnyersK 能够得到一个更简单但类似的场景.所以我尝试了同样的事情,但我仍然得到同样的异常.它在检索数组时将我的测试数组转换为对象,从而导致强制转换异常.

SnyersK was able to get a simpler but similar scenario to work. So I tried the same thing, and I still get the same exception. It turned my array of Tests into an Object when retrieving the array, which results in the casting exception.

我的测试对象:

package types;

import java.io.Serializable;

public class Test implements Serializable {

    private static final long serialVersionUID = 1L;

    private String hello = "hello";

}

传递测试对象数组的代码:

Test myArray[] = new Test[] { new Test(), new Test() };
Intent i = new Intent( this, Login.class );
i.putExtra( "key", myArray );

检索测试对象数组的代码:

Bundle extras = getIntent().getExtras();
Test array[] = (Test[]) extras.getSerializable( "key" ); //Class Cast Exception

推荐答案

显然这是旧版 Android 上的错误.(版本 5.0.1 是唯一经过测试的版本,它可以正常工作,也许它可以从 5.0 及更高版本开始运行)

Apparently this is a bug on older versions of Android. (Version 5.0.1 is the only version tested on which it works like expected, perhaps it works from 5.0 and up)

错误

假设我们有一个名为 User 的对象数组,它实现了 Serializable.

Let's say we have an Array of an object called User which implements Serializable.

User[] users;

当我们尝试通过这样的意图将这个数组传递给另一个活动时

When we try to pass this array to another activity through an intent like this

intent.putExtra("myKey", users);

我们的users 将被转换为Object[].

如果我们尝试从第二个活动中的意图中获取我们的数组

If we try to get our array from the intent in the second activity like so

User[] users = getIntent().getSerializableExtra("myKey");

我们会得到一个 ClassCastException.

We will get a ClassCastException.

从文档中,我们可以得出结论,这应该不是问题,因为 Array 是一个可序列化的类,而 putExtra(String key, Serializable value) 已经被从 api 1 开始添加.

From the docs, we can conclude that this shouldn't be a problem since Array is a serializable class, and putExtra(String key, Serializable value) has been added since api 1.

如果您想在 Android 5.0.1 之前的 Android 版本上通过 Intent 传递 Array(也许一些旧版本也可以使用,但在 Android 4.3 之前它不起作用)你必须解决它.您可以通过将 Array 转换为 ArrayList 来实现.

If you want to pass an Array through an intent on Android versions prior to Android 5.0.1 (maybe some older versions work aswell, but up to Android 4.3 it is NOT working) You'll have to work around it. You could do this by converting your Array to an ArrayList.

编辑

另一种解决方法是将额外的内容复制到新数组中,因此不必强制转换.不过,我不确定该方法的积极/消极后果.它看起来像这样:

another workaround is copying your extra to a new array, thus, not having to cast it. I'm not sure about the positive/negative consequences of the method though. It would look like this:

Object[] array = (Object[]) getIntent().getSerializableExtra("key");
User[] parsedArray = Arrays.copyOf(array, array.length, User[].class);

我在这里找到了这个方法:如何转换Java中的对象数组到字符串数组.

I found this method here: How to convert object array to string array in Java.

这篇关于将 Serializable 数组从一个活动传递到另一个活动时出现类 Cast 异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆