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)

最佳答案

您应该查看控制台日志。重点关注它告诉您的错误类型:


  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列表的值。

08-18 03:19