序列化/反序列化LinkedHashMap中(机器人)的java [英] serialize/deserialize a LinkedHashMap (android) java
问题描述
所以我想LinkedHashMap中传递到意向。
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");
这一次的工作就像用HashMap中的魅力。 但随着LinkedHashMap中我有一个问题,我在这里得到了一个运行时错误
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");
我没有得到任何错误使用HashMap中。
I get no error with HashMap.
我也得到了一个警告类型的安全:未选中铸造而成的序列化到的LinkedHashMap 但我有这样的警告与HashMap的了。 任何ideas.Any帮助是非常AP preciated
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
此外,我刚才看到这一点。 <一href="https://issues.apache.org/jira/browse/HARMONY-6498">https://issues.apache.org/jira/browse/HARMONY-6498
Also I just saw this. 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.
泛型的运行时类型是原始类型;即类型,其中的实际类型的类型参数是未知的。在这种情况下,运行时类型为的LinkedHashMap&LT;,&GT;
。这不能安全地强制转换为 LinkedMashMap&LT;字符串,字符串&GT;
因为运行时系统没有办法知道,所有的键和值实际上是字符串
实例。
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 to LinkedMashMap<String, String>
because the runtime system has no way of knowing that all of the keys and values are actually String
instances.
您有两种选择:
-
您可以添加注释来告诉编译器闭嘴关于选中演员。这是一个有点冒险。如果由于某种原因键或值实际上不是一个字符串,你的应用程序的可以的后获得
ClassCastException异常
在一个完全意想不到的地方;例如而迭代的键或分配GET
。
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
.
您可以手动复制deserialised 的LinkedHashMap&LT;,&GT;
进入一个新的 LinkedMashMap&LT;字符串,字符串&GT;
,明确铸件每个键和值字符串
。
You could manually copy the deserialised LinkedHashMap<?, ?>
into a new LinkedMashMap<String, String>
, explicitly casting each key and value to String
.
修改
如果您使用的是和谐CLASSLIB或Sun与和谐的混合物,然后运行时异常可能是由和谐的错误解释。但是,这是不可能说没有看到异常,并在您使用序列化和反序列化映射哪些类/版本precisely更多细节。
If you are using the Harmony classlib or a mixture of Sun and Harmony, then the runtime exception might be explained by bugs in Harmony. But it is impossible to tell without seeing the exception, and more details on precisely which classes / versions you are using to serialize and deserialize the maps.
这篇关于序列化/反序列化LinkedHashMap中(机器人)的java的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!