我正在使用 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)
我想将这两个值添加到图形中 . 我也尝试在double中使用这些值但仍然是错误,如:
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)
2 回答
根据您的评论,如果您说您正在获取这些值但仍然出现错误,是因为Firebase API是异步的,这意味着在您创建以下对象时:
来自数据库的数据尚未完成加载 . 快速解决这个问题,就是移动所有这些代码行:
在
onDataChange()
方法内部,否则我建议你从这个 post 看到我的anwser的最后一部分,其中我已经解释了如何使用自定义回调来完成它 . 你也可以看看这个 video 以便更好地理解 .您必须在指定引用时给出完整引用,否则您获得的数据快照将为null.Hence而不是
realdata.getReference("weight");你必须指定
realdata.getReferenceFromUrl( "https://your-project-id/weight")
如果weight没有任何parentnode,则在url中指定它
getReferenceFromUrl( "https://your-project-id/parent/child1/child2..")
希望这可以帮助!