简体   繁体   English

序列化/反序列化LinkedHashMap(android)java

[英]serialize/deserialize a LinkedHashMap (android) java

So i want to pass a LinkedHashMap to an intent. 所以我想将LinkedHashMap传递给一个意图。

//SEND THE MAP
Intent singlechannel = new Intent(getBaseContext(),singlechannel.class);
singlechannel.putExtra("db",shows1);//perase to
startActivity(singlechannel);

//GET THE MAP
LinkedHashMap<String,String> db = new LinkedHashMap<String,String>();   
db=(LinkedHashMap<String,String>) getIntent().getSerializableExtra("db");

This one Worked Like a charm with HashMap. HashMap就像一个魅力一样工作。 But with LinkedHashMap i got a problem i got a runtime error here 但是使用LinkedHashMap我遇到了一个问题,我在这里遇到了运行时错误

  db=(LinkedHashMap<String,String>) getIntent().getSerializableExtra("db");

I get no error with HashMap. 我没有HashMap错误。

I also got a warning "Type safety: Unchecked cast from Serializable to LinkedHashMap" But i had this warning with HashMap too. 我也收到警告“类型安全:未经检查的从Serializable到LinkedHashMap的转换”,但我也对HashMap发出此警告。 Any ideas.Any help is much appreciated 任何想法。非常感谢您的帮助

Also I just saw this. 我也看到了。 https://issues.apache.org/jira/browse/HARMONY-6498 https://issues.apache.org/jira/browse/HARMONY-6498

The root of the problem here is that you are trying to type cast to a generic type. 问题的根源在于您试图将类型转换为通用类型。 This cannot be done without an unsafe/unchecked type cast. 如果没有不安全/未经检查的类型转换,则无法完成此操作。

The runtime types of generic types are raw types; 泛型类型的运行时类型是原始类型。 ie types in which the actual types of the type parameters are not known. 即类型参数的实际类型未知的类型。 In this case the runtime type will be LinkedHashMap<?, ?> . 在这种情况下,运行时类型将为LinkedHashMap<?, ?> This cannot be safely typecast to LinkedMashMap<String, String> because the runtime system has no way of knowing that all of the keys and values are actually String instances. 无法安全地将其强制类型转换为LinkedMashMap<String, String>因为运行时系统无法知道所有键和值实际上都是String实例。

You have two options: 您有两种选择:

  • You could add an annotation to tell the compiler to "shut up" about the unchecked cast. 您可以添加注释,以告知编译器“关闭”未经检查的演员表。 This is a bit risky. 这有点冒险。 If for some reason one of the keys or values is not actually a String, your application may later get a ClassCastException at a totally unexpected place; 如果出于某种原因,这些键或值之一实际上不是String,那么您的应用程序以后可能会在完全意外的地方收到ClassCastException eg while iterating the keys or assigning the result of a get . 例如,在迭代键或分配get结果时。

  • You could manually copy the deserialised Map<?, ?> into a new LinkedMashMap<String, String> , explicitly casting each key and value to String . 您可以手动将反序列化的Map<?, ?>复制到新的LinkedMashMap<String, String> ,将每个键和值显式转换为String

UPDATE 更新

Apparently the real cause of the runtime exception (as distinct from the "unsafe typecast" compilation error) is that Android is passing the serializedExtra between intents as a regular HashMap rather than using the Map class that you provided. 显然,造成运行时异常的真正原因(不同于“不安全的类型转换”编译错误)是Android将常规意图之间的serializedExtra作为常规HashMap传递,而不是使用您提供的Map类。 This is described here: 此处描述:

As that Q&A explains, there is no simple workaround. 正如该问答所解释的那样,没有简单的解决方法。 If you want to pass a map preserving the key order, will have to convert the map to an array of pairs and pass that. 如果要传递保留键顺序的映射,则必须将映射转换为成对数组并传递。 On the other end, you will then need to reconstruct the map from the passed array. 另一方面,您将需要从传递的数组中重建映射。

LinkedHashMap does implement Map interface, here is definition from the source code: LinkedHashMap确实实现了Map接口,这是源代码中的定义:

public class LinkedHashMap<K,V>
    extends HashMap<K,V>
    implements Map<K,V>

The following test is working fine, so I think the problem you have is not related LinkedHashMap. 以下测试工作正常,因此我认为您遇到的问题与LinkedHashMap没有关系。

Map<String, String> map = new LinkedHashMap<String, String>();
        map.put("1", "one");
        FileOutputStream fos = new FileOutputStream("c://map.ser");
        ObjectOutputStream out = new ObjectOutputStream(fos);
        out.writeObject(map);
        out.close();


        FileInputStream fin = new FileInputStream("c://map.ser");
        ObjectInputStream in = new ObjectInputStream(fin);
        Map<String, String> map2 = (Map<String, String>) in.readObject();
        System.out.println(map2); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM