序列化/反序列化 LinkedHashMap (android) java
所以我想将 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");
这个与 HashMap 一起工作就像一个魅力。 但是使用 LinkedHashMap 我遇到了一个问题,我在这里遇到运行时错误,但
db=(LinkedHashMap<String,String>) getIntent().getSerializableExtra("db");
HashMap 没有错误。
我还收到警告“类型安全:从 Serialized 到 LinkedHashMap 的未经检查的转换” 但我对 HashMap 也有这个警告。 任何想法。任何帮助我都非常感激
我也刚刚看到这个。 https://issues.apache.org/jira/browse/HARMONY-6498
So i want to pass a LinkedHashMap to an intent.
//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.
But with LinkedHashMap i got a problem i got a runtime error here
db=(LinkedHashMap<String,String>) getIntent().getSerializableExtra("db");
I get no error with HashMap.
I also got a warning "Type safety: Unchecked cast from Serializable to LinkedHashMap"
But i had this warning with HashMap too.
Any ideas.Any help is much appreciated
Also I just saw this.
https://issues.apache.org/jira/browse/HARMONY-6498
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这里问题的根源是您试图将类型转换为泛型类型。如果没有不安全/未经检查的类型转换,这是无法完成的。
泛型类型的运行时类型是原始类型;即类型参数的实际类型未知的类型。在这种情况下,运行时类型将为
LinkedHashMap
。这不能安全地类型转换为LinkedMashMap
,因为运行时系统无法知道所有键和值实际上都是String
实例。您有两个选择:
您可以添加注释来告诉编译器“关闭”未经检查的强制转换。这有点冒险。如果由于某种原因,其中一个键或值实际上不是字符串,您的应用程序可能稍后会在完全意想不到的地方收到
ClassCastException
;例如,在迭代键或分配get
的结果时。您可以手动将反序列化的
Map
复制到新的LinkedMashMap
中,将每个键和值显式转换为String
。更新
显然,运行时异常的真正原因(与“不安全类型转换”编译错误不同)是 Android 将
serializedExtra
作为常规在意图之间传递HashMap
而不是使用您提供的Map
类。此处对此进行了描述:正如问答所解释的那样,没有简单的解决方法。如果您想传递保留键顺序的映射,则必须将映射转换为对数组并传递它。另一方面,您需要根据传递的数组重建地图。
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; i.e. types in which the actual types of the type parameters are not known. In this case the runtime type will be
LinkedHashMap<?, ?>
. This cannot be safely typecast toLinkedMashMap<String, String>
because the runtime system has no way of knowing that all of the keys and values are actuallyString
instances.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; e.g. while iterating the keys or assigning the result of aget
.You could manually copy the deserialised
Map<?, ?>
into a newLinkedMashMap<String, String>
, explicitly casting each key and value toString
.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 regularHashMap
rather than using theMap
class that you provided. 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确实实现了Map接口,这里是源代码中的定义:
以下测试工作正常,所以我认为您遇到的问题与LinkedHashMap无关。
LinkedHashMap does implement Map interface, here is definition from the source code:
The following test is working fine, so I think the problem you have is not related LinkedHashMap.