我很难将数据从firebase计算到我制作的饼图中。我有这个奇怪的错误,我不知道它是什么。
我尝试计算它并将其解析为浮动,以确定它是什么。
这是我用于在piechard中添加值的方法
public void AddValuesToPIEENTRY(){
mUsersDatabase = FirebaseDatabase.getInstance().getReference().child("Statistics");
mUsersDatabase.child("positive").addListenerForSingleValueEvent(new ValueEventListener() {
int countpositive = 0;
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (dataSnapshot.exists()){
for (DataSnapshot dataw : dataSnapshot.getChildren()) {
countpositive = countpositive + 1;
}
}
entries.add(new BarEntry(Float.parseFloat((countpositive+"f")), 0));
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
mUsersDatabase.child("negative").addListenerForSingleValueEvent(new ValueEventListener() {
int countnegative = 0;
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (dataSnapshot.exists()){
for (DataSnapshot dataw : dataSnapshot.getChildren()) {
countnegative = countnegative + 1;
}
}
entries.add(new BarEntry(Float.parseFloat(countnegative+"f"), 1));
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
这是我的错误
Process: com.example.masternarding.redcrossapp, PID: 6398
java.lang.ArrayIndexOutOfBoundsException: length=0; index=0
at com.github.mikephil.charting.renderer.PieChartRenderer.drawDataSet(PieChartRenderer.java:205)
at com.github.mikephil.charting.renderer.PieChartRenderer.drawData(PieChartRenderer.java:126)
at com.github.mikephil.charting.charts.PieChart.onDraw(PieChart.java:123)
at android.view.View.draw(View.java:15122)
at android.view.View.updateDisplayListIfDirty(View.java:14056)
at android.view.View.getDisplayList(View.java:14079)
at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:3389)
at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3368)
at android.view.View.updateDisplayListIfDirty(View.java:14016)
at android.view.View.getDisplayList(View.java:14079)
at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:3389)
at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3368)
at android.view.View.updateDisplayListIfDirty(View.java:14016)
at android.view.View.getDisplayList(View.java:14079)
at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:3389)
at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3368)
at android.view.View.updateDisplayListIfDirty(View.java:14016)
at android.view.View.getDisplayList(View.java:14079)
at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:3389)
请在此代码中更正我的错误行为。 我今天的演讲需要这个,帮助我们。
答案 0 :(得分:0)
不幸的是,MPAndroid不支持Firebase提供的实时数据。导致此错误,因为您的条目为空。一种解决方法是从您来到此活动的活动中从firebase获取数据,并通过data-callback
发送数据,如:
intent.putextras()
这不会让MPAndroid处理实时数据并解决您的问题。