[英]java.lang.Long cannot be cast to java.lang.Integer
I want to read a map map
from the Database (Firebase),and i compare it to another map motcle
, if i find a duplicate key i increment the value of the key in map
,I am having a ClassCastException. 我想从数据库(Firebase)中读取一个地图
map
,并将其与另一个地图motcle
进行比较,如果我发现重复的钥匙,我会增加map
钥匙的值,那么我就遇到了ClassCastException。 the code: 编码:
public void addMotcle(final Map motcle, String Userid)
{
mDatabase = FirebaseDatabase.getInstance().getReference("User");
DatabaseReference user = mDatabase.child(Userid);
final DatabaseReference keylist = user.child("motcle");
keylist.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
if(dataSnapshot.exists())
{
//I tried to put the map in another map and change it but
//It is still the same error
Map<String,Integer> map = (Map) dataSnapshot.getValue();
Map<String,Integer>Hmap= new HashMap<>();
Hmap.putAll(map);
//if there is duplicate we incremente les mots clé
for (Object key : motcle.keySet()) {
if (Hmap.containsKey(key)) {
Hmap.put((String)key,((Integer) Hmap.get(key))+1);
}
else
{
Hmap.put((String) key,1);
}
}
keylist.setValue(Hmap);
}
else
{
keylist.setValue(motcle);
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
Here is the Value that i want to write on in Firebase 这是我要在Firebase中写的值
the error is in the line map.put((String)key,map.get(key)+1);
错误在
map.put((String)key,map.get(key)+1);
why can't i write an Integer
in the value if i declared map
as Map<String,Integer>
? 如果我将
map
声明为Map<String,Integer>
为什么不能在值中写一个Integer
?
When you do Map<String,Integer> map = (Map) dataSnapshot.getValue();
当您执行
Map<String,Integer> map = (Map) dataSnapshot.getValue();
, you guarantee that map
is a Map
, but not that it's a Map<String, Integer>
. ,您可以保证
map
是Map
,但不能保证它是Map<String, Integer>
。 Since generics are erased and not available at runtime, as long as dataSnapshot.getValue()
is a Map
of some kind, this cast will succeed. 由于泛型会被擦除并且在运行时不可用,只要
dataSnapshot.getValue()
是某种Map
,此转换就可以成功。
Because you've declared Hmap
as a Map<String,Integer>
, when you get a value from the map, it will try to cast it to Integer
, but there is nothing actually guaranteeing ahead-of-time that it will be an Integer . 因为您已将
Hmap
声明为Map<String,Integer>
,所以当您从地图中获取值时,它将尝试将其Hmap
为Integer
,但实际上并没有任何保证能提前保证它将成为Integer。 。 As a result, if the value is actually a Long
, it will try to cast that Long
to an Integer
and fail (because Long
does not extend Integer
, and widening operations don't work on boxed types). 结果,如果该值实际上是
Long
,它将尝试将Long
转换为Integer
并失败(因为Long
不会扩展Integer
,并且扩展操作不适用于盒装类型)。
You need to find out what the actual types of the values of dataSnapshot.getValue()
are, and cast it to the correct type of Map
to avoid this error. 您需要找出
dataSnapshot.getValue()
值的实际类型是什么,并将其dataSnapshot.getValue()
转换为正确的Map
类型,以避免此错误。
See here for more details: Weird problem about Java Generics operation 有关更多详细信息,请参见此处: 关于Java泛型操作的奇怪问题
这是因为键列表是一个最终变量,您无法更改其值,因此可以像这样初始化它:-
DatabaseReference keylist = user.child("motcle");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.