绘图图 // 无法将 java.util.HashMap 类型的值转换为字符串



我尝试用Firebase的数据绘制一个图表。在此错误之前,它显示 y 轴(带有我自己创建的 x 轴(。但是当我尝试使用来自 firebase 的数据绘制 x 轴时(在 firebase 中是字符串类型(,会发生此错误。

com.google.firebase.database.DatabaseException: Failed to convert value of type java.util.HashMap to String
at com.google.firebase.database.core.utilities.encoding.CustomClassMapper.convertString(com.google.firebase:firebase-database@@19.2.0:425)
at com.google.firebase.database.core.utilities.encoding.CustomClassMapper.deserializeToClass(com.google.firebase:firebase-database@@19.2.0:216)
at com.google.firebase.database.core.utilities.encoding.CustomClassMapper.deserializeToType(com.google.firebase:firebase-database@@19.2.0:178)
at com.google.firebase.database.core.utilities.encoding.CustomClassMapper.access$100(com.google.firebase:firebase-database@@19.2.0:47)
at com.google.firebase.database.core.utilities.encoding.CustomClassMapper$BeanMapper.deserialize(com.google.firebase:firebase-database@@19.2.0:603)
at com.google.firebase.database.core.utilities.encoding.CustomClassMapper$BeanMapper.deserialize(com.google.firebase:firebase-database@@19.2.0:562)
at com.google.firebase.database.core.utilities.encoding.CustomClassMapper.convertBean(com.google.firebase:firebase-database@@19.2.0:432)
at com.google.firebase.database.core.utilities.encoding.CustomClassMapper.deserializeToClass(com.google.firebase:firebase-database@@19.2.0:231)
at com.google.firebase.database.core.utilities.encoding.CustomClassMapper.convertToCustomClass(com.google.firebase:firebase-database@@19.2.0:79)
at com.google.firebase.database.DataSnapshot.getValue(com.google.firebase:firebase-database@@19.2.0:203)
at com.example.smartbin.GraphActivity$1.onDataChange(GraphActivity.java:62)

这是我的代码似乎有问题

database=FirebaseDatabase.getInstance();
reference = database.getReference("test").orderByChild("DevEUI_uplink/DevAddr").equalTo("AB000001");
}
@Override
protected void onStart() {
super.onStart();
reference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
DataPoint[] dp=new DataPoint[(int) dataSnapshot.getChildrenCount()];
int index=0;
for (DataSnapshot myDataSnapshot : dataSnapshot.getChildren()){
PointValue pointValue = myDataSnapshot.child("DevEUI_uplink/payload_parsed/frames/1").getValue(PointValue.class);
PointValue pointValue2 = myDataSnapshot.child("DevEUI_uplink").getValue(PointValue.class); //GraphActivity.java:62
dp[index]= new DataPoint(pointValue2.getFCntUp(),pointValue.getyValue());
index++;
}

这是我来自 PointValue 的代码.class 我也尝试将数据从字符串转换为整数,但不确定我做得是否正确。

public void setValue(int value)
{
this.value = value;
}
public int getValue()
{
return value;
}
public void setFCntUp(String FCntUp){
this.FCntUp = FCntUp;
}
public int getFCntUp(){
return Integer.parseInt(FCntUp);
}

我在PointValue中删除了Integer.parseint.class然后只得到了String。我从 PointValue 获取字符串.class并在我的 MainActivity 中转向 Int。

for (DataSnapshot myDataSnapshot : dataSnapshot.getChildren()){
PointValue pointValue = myDataSnapshot.child("DevEUI_uplink/payload_parsed/frames/1").getValue(PointValue.class);
PointValue pointValue2 = myDataSnapshot.child("DevEUI_uplink").getValue(PointValue.class);
nvalue = pointValue2.getFCntUp();
x = Integer.parseInt(nvalue);
y = pointValue.getValue();
dp[index]= new DataPoint(x,y);
index++;
}
series.resetData(dp);
}`

这是我的点值.class

public void setFCntUp(String FCntUp)
{
this.FCntUp = FCntUp;
}
public String getFCntUp()
{
return FCntUp;
}

public void setValue(int value)
{
this.value = value;
}
public int getValue()
{
return value;
}

不确定这是否是问题的解决方案,但它对我有用。

相关内容

最新更新