如何将firebase中的数据动态添加到graphview中



我使用jjoe64.graphview.graphview库绘制图形。

我正在附加一些来自firebase数据库的数据,但它显示了我的错误。

这是我的代码:

FirebaseDatabase realdata = FirebaseDatabase.getInstance();
DatabaseReference real = realdata.getReference("weight");
ValueEventListener workplz = new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
//point value=62,pointvalue1=76
pointValue = Integer.parseInt(String.valueOf(dataSnapshot.child("currentweight").getValue()));
pointValue1 = Integer.parseInt(String.valueOf(dataSnapshot.child("goalweight").getValue()));
Log.i("xValue", String.valueOf(pointValue));
Log.i("yValue", String.valueOf(pointValue1));

valuedata = Double.parseDouble(String.valueOf(dataSnapshot.child("currentweight").getValue()));
hello();
}
public void onCancelled(@NonNull DatabaseError databaseError) {
}};
real.addValueEventListener(workplz);
GraphView graph = findViewById(R.id.graph);
LineGraphSeries<DataPoint> series = new LineGraphSeries<>(new DataPoint[]{
new DataPoint(0, 1),
new DataPoint(1, 5),
new DataPoint(2, 3)
});
graph.addSeries(series);
graph.getViewport().setXAxisBoundsManual(true);
graph.getViewport().setYAxisBoundsManual(true);
graph.getViewport().setMinY(0);
graph.getViewport().setMaxY(200);
graph.getViewport().setMaxX(0);
graph.getViewport().setMaxX(200);
//appending data 
series.appendData(new DataPoint(pointValue, pointValue1), true, 200);

在运行应用程序时,我遇到了此错误。

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'int java.lang.Integer.intValue()' on a null object reference
at com.example.itsandpro.Signup.GraphActivity.onCreate(GraphActivity.java:97)
at android.app.Activity.performCreate(Activity.java:6679)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2618)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726) 
at android.app.ActivityThread.-wrap12(ActivityThread.java) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:154) 
at android.app.ActivityThread.main(ActivityThread.java:6119) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776) 

我想把这两个值加到图上。我也试着用双倍的值,但错误仍然是一样的:

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'double java.lang.Double.doubleValue()' on a null object reference
at com.example.itsandpro.Signup.GraphActivity.onCreate(GraphActivity.java:85)
at android.app.Activity.performCreate(Activity.java:6679)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2618)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726) 
at android.app.ActivityThread.-wrap12(ActivityThread.java) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:154) 
at android.app.ActivityThread.main(ActivityThread.java:6119) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776) 

根据您的评论,您说您正在获得这些值,但仍然获得错误,这是因为Firebase API是异步的吗?这意味着当您创建以下对象时:

new DataPoint(pointValue, pointValue1), true, 200

来自数据库的数据尚未完成加载。对此的一个快速解决方案是移动所有这些代码行:

GraphView graph = findViewById(R.id.graph);
LineGraphSeries<DataPoint> series = new LineGraphSeries<>(new DataPoint[]{
new DataPoint(0, 1),
new DataPoint(1, 5),
new DataPoint(2, 3)
});
graph.addSeries(series);
graph.getViewport().setXAxisBoundsManual(true);
graph.getViewport().setYAxisBoundsManual(true);
graph.getViewport().setMinY(0);
graph.getViewport().setMaxX(0);
graph.getViewport().setMaxX(200);
//appending data 
series.appendData(new DataPoint(pointValue, pointValue1), true, 200);

onDataChange()方法内部,否则我建议您查看文章中我的anwser的最后一部分,我在其中解释了如何使用自定义回调来完成它。您也可以查看此视频以更好地理解。

最新更新