gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 01:15:43 26 4
gpt4 key购买 nike

我对 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; index=0

这意味着您正在尝试访问列表的错误位置,noSQLEntities

您在这一行收到错误:

BarEntry entry = new BarEntry(i, item);

但我几乎可以肯定你的错误确实在另一行(在上面的行中使用):

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

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

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

关于android - 尝试在 BarEntry 数组上添加值时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39796645/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com