使用实时数据绘制超过2行时获取NullPointerException

时间:2016-02-19 17:21:41

标签: android nullpointerexception mpandroidchart

我尝试使用MpAndroidChart绘制折线图,​​并在运行时生成值。图表中的行数也可以在运行时添加或删除。我使用以下代码来执行此操作

private void addEntry(String label, float xValue, float yValue) {
    LineData data = mChart.getData();

    if (data != null) {

        ILineDataSet set = data.getDataSetByLabel(label, false);
        // set.addEntry(...); // can be called as well

        if (set == null) {
            set = createSet(label);
            data.addDataSet(set);
        }

        // add a new x-value first
        data.addXValue(String.valueOf(xValue));

        set.addEntry(new Entry(yValue, set.getEntryCount()));
        data.notifyDataChanged();

        // let the chart know it's data has changed
        mChart.notifyDataSetChanged();

        // move to the latest entry
        mChart.moveViewToX(data.getXValCount() - 121);
}

private LineDataSet createSet(String label) {
    LineDataSet set = new LineDataSet(null, label);
    set.setAxisDependency(YAxis.AxisDependency.LEFT);
    Random random = new Random();
    set.setColor(Color.argb(255, random.nextInt(256), random.nextInt(256), random.nextInt(256)));
    set.setCircleColor(Color.WHITE);
    set.setLineWidth(2f);
    set.setCircleRadius(1f);
    set.setFillAlpha(65);
    set.setFillColor(ColorTemplate.getHoloBlue());
    set.setHighLightColor(Color.rgb(244, 117, 117));
    set.setValueTextColor(Color.WHITE);
    set.setValueTextSize(9f);
    set.setDrawValues(false);
    return set;
}

它与库{/ 3}}中的代码几乎相同。当图表中只绘制一条线时,没有问题。但是,当使用实时数据同时绘制超过2或3行时会出现问题。图表为某些值绘制线条,然后因以下错误而崩溃:

FATAL EXCEPTION: main
   java.lang.NullPointerException: Attempt to invoke virtual method 'void com.github.mikephil.charting.buffer.LineBuffer.setPhases(float, float)' on a null object reference
   at com.github.mikephil.charting.renderer.LineChartRenderer.drawLinear(LineChartRenderer.java:309)
   at com.github.mikephil.charting.renderer.LineChartRenderer.drawDataSet(LineChartRenderer.java:129)
   at com.github.mikephil.charting.renderer.LineChartRenderer.drawData(LineChartRenderer.java:108)
   at com.github.mikephil.charting.charts.BarLineChartBase.onDraw(BarLineChartBase.java:251)
   at android.view.View.draw(View.java:16187)
   at android.view.View.updateDisplayListIfDirty(View.java:15184)
   at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:3593)
   at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3573)
   at android.view.View.updateDisplayListIfDirty(View.java:15144)
   at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:3593)
   at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3573)
   at android.view.View.updateDisplayListIfDirty(View.java:15144)
   at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:3593)
   at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3573)
   at android.view.View.updateDisplayListIfDirty(View.java:15144)
   at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:3593)
   at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3573)
   at android.view.View.updateDisplayListIfDirty(View.java:15144)
   at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:3593)
   at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3573)
   at android.view.View.updateDisplayListIfDirty(View.java:15144)
   at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:3593)
   at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3573)
   at android.view.View.updateDisplayListIfDirty(View.java:15144)
   at android.view.ThreadedRenderer.updateViewTreeDisplayList(ThreadedRenderer.java:281)
   at android.view.ThreadedRenderer.updateRootDisplayList(ThreadedRenderer.java:287)
   at android.view.ThreadedRenderer.draw(ThreadedRenderer.java:322)
   at android.view.ViewRootImpl.draw(ViewRootImpl.java:2615)
   at android.view.ViewRootImpl.performDraw(ViewRootImpl.java:2434)
   at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2067)
   at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1107)
   at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6013)
   at android.view.Choreographer$CallbackRecord.run(Choreographer.java:858)
   at android.view.Choreographer.doCallbacks(Choreographer.java:670)
   at android.view.Choreographer.doFrame(Choreographer.java:606)
   at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:844)
   at android.os.Handler.handleCallback(Handler.java:739)
   at android.os.Handler.dispatchMessage(Handler.java:95)
   at android.os.Looper.loop(Looper.java:148)
   at android.app.ActivityThread.main(ActivityThread.java:5466)
   at java.lang.reflect.Method.invoke(Native Method)
   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

这是什么问题?我得到的例外是来自库代码 非常感谢任何帮助。

0 个答案:

没有答案