尝试在BarEntry数组上添加值时出错

时间:2016-09-30 17:09:26

标签: android bar-chart mpandroidchart

我对MPAndroidChart有点问题。在上面的代码中,我得到一个错误,让我疯了3天。 我尝试将条目添加到barEntry数组时出现错误,我不知道为什么。

private void retrive(){
    NoSQL.with(this).using(Float.class)
            .bucketId("MyMarket")
            .entityId("Chart")
            .retrieve(new RetrievalCallback<Float>() {
                @Override
                public void retrievedResults(List<NoSQLEntity<Float>> noSQLEntities) {
                    for(int i = 0; i < noSQLEntities.size(); i++) {
                        float item = noSQLEntities.get(i).getData();
                        BarEntry entry = new BarEntry(i, item);
                        barEntries.add(entry);

                    }
                }
            });

    BarDataSet barDataSet = new BarDataSet(barEntries, "Compras");

    BarData theData = new BarData(barDataSet);
    theData.setBarWidth(0.9f);
    barChart.setData(theData);

    barChart.setTouchEnabled(true);
    barChart.setDragEnabled(true);
    barChart.setScaleEnabled(true);
    barChart.setDescription("");
    barChart.animateY(2700);
    barChart.setData(theData);
    barChart.setFitBars(true);

    XAxis xAxis = barChart.getXAxis();
    xAxis.setDrawGridLines(false);

    YAxis right = barChart.getAxisRight();
    right.setDrawGridLines(false);

    YAxis left = barChart.getAxisLeft();
    left.setDrawGridLines(false);

    barChart.invalidate();
}

在这一行:

BarEntry entry = new BarEntry(i, item);

我确定已添加该值,但程序会一直给我这个错误:

E/AndroidRuntime: FATAL EXCEPTION: main
              Process: com.stark.mymarket, PID: 6404
              java.lang.ArrayIndexOutOfBoundsException: length=0; index=0
                  at com.github.mikephil.charting.buffer.BarBuffer.addBar(BarBuffer.java:37)
                  at com.github.mikephil.charting.buffer.BarBuffer.feed(BarBuffer.java:80)
                  at com.github.mikephil.charting.renderer.BarChartRenderer.drawDataSet(BarChartRenderer.java:103)
                  at com.github.mikephil.charting.renderer.BarChartRenderer.drawData(BarChartRenderer.java:78)
                  at com.github.mikephil.charting.charts.BarLineChartBase.onDraw(BarLineChartBase.java:243)
                  at android.view.View.draw(View.java:16178)
                  at android.view.View.updateDisplayListIfDirty(View.java:15174)
                  at android.view.View.draw(View.java:15948)
                  at android.view.ViewGroup.drawChild(ViewGroup.java:3609)
                  at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3399)
                  at android.view.View.updateDisplayListIfDirty(View.java:15169)
                  at android.view.View.draw(View.java:15948)
                  at android.view.ViewGroup.drawChild(ViewGroup.java:3609)
                  at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3399)
                  at android.view.View.updateDisplayListIfDirty(View.java:15169)
                  at android.view.View.draw(View.java:15948)
                  at android.view.ViewGroup.drawChild(ViewGroup.java:3609)
                  at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3399)
                  at android.view.View.updateDisplayListIfDirty(View.java:15169)
                  at android.view.View.draw(View.java:15948)
                  at android.view.ViewGroup.drawChild(ViewGroup.java:3609)
                  at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3399)
                  at android.view.View.updateDisplayListIfDirty(View.java:15169)
                  at android.view.View.draw(View.java:15948)
                  at android.view.ViewGroup.drawChild(ViewGroup.java:3609)
                  at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3399)
                  at android.view.View.updateDisplayListIfDirty(View.java:15169)
                  at android.view.View.draw(View.java:15948)
                  at android.view.ViewGroup.drawChild(ViewGroup.java:3609)
                  at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3399)
                  at android.view.View.draw(View.java:16181)
                  at com.android.internal.policy.PhoneWindow$DecorView.draw(PhoneWindow.java:2690)
                  at android.view.View.updateDisplayListIfDirty(View.java:15174)
                  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:5417)
                  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)

1 个答案:

答案 0 :(得分:1)

您应该查看控制台日志。专注于它告诉你的错误:

  

java.lang.ArrayIndexOutOfBoundsException:length = 0;索引= 0

表示您尝试访问列表中的错误位置noSQLEntities

您收到此行的错误:

BarEntry entry = new BarEntry(i, item);

但我几乎可以确定你的错误确实存在于另一条线路上(在上面的线路上使用):

float item = noSQLEntities.get(i).getData();

您似乎没有使用值初始化noSQLEntities列表,因此您尝试访问该列表中不存在的位置。

确保您的noSQLEntities列表包含值。您可以使用debug mode(如果您使用的是Android Studio)或logs来查看控制台中noSQLEntities列表的值。