gpt4 book ai didi

android - 将图表引擎的圆环图 View 添加到线性布局时出现 IndexOutOfBoundsException

转载 作者:行者123 更新时间:2023-11-29 00:28:29 24 4
gpt4 key购买 nike

我想在我的 Activity 中显示圆环图。事实上,我的 Activity 由一些 fragment 组成。所以它不扩展 Activity 但它扩展了 FragmentActivity。我正在使用 achartengine 的 doughnutchartview 通过将它添加到线性布局来将圆环图集成到 Activity 中(而不是在它的 fragment 中)。线性布局已经在 xml 布局文件中定义。这是我的代码:

Activity :

public class FoodConfessionsActivity extends FragmentActivity { 
GraphicalView mChartView;
DefaultRenderer mRenderer;
MultipleCategorySeries mSeries;
...

@Override
protected void onResume() {
super.onResume();
if (mChartView == null) {
LinearLayout chart1 = (LinearLayout) findViewById(R.id.chart1);
List<double[]> values = new ArrayList<double[]>();
values.add(new double[] { 12, 14, 11, 10, 19 });
values.add(new double[] { 10, 9, 14, 20, 11 });
List<String[]> titles = new ArrayList<String[]>();
titles.add(new String[] { "P1", "P2", "P3", "P4", "P5" });
titles.add(new String[] { "Project1", "Project2", "Project3", "Project4", "Project5" });
int[] colors = new int[] { Color.BLUE, Color.GREEN, Color.MAGENTA, Color.YELLOW, Color.CYAN };
mSeries = new MultipleCategorySeries("ideal");
int k = 0;
for (double[] value : values) {
mSeries.add(2007 + k + "", titles.get(k), value);
k++;
}
mRenderer = new DefaultRenderer();
mRenderer.setApplyBackgroundColor(true);
mRenderer.setBackgroundColor(Color.rgb(222, 222, 200));
mRenderer.setLabelsColor(Color.GRAY);
mRenderer.setStartAngle(180);
mRenderer.setDisplayValues(true);
mChartView = ChartFactory.getDoughnutChartView(getApplicationContext(), mSeries, mRenderer);
chart1.addView(mChartView); // Exception occurs at this line

} else {
mChartView.repaint();
}
}
}

xml布局:

<?xml version="1.0" encoding="utf-8"?>
...
<LinearLayout
android:id="@+id/chart1"
android:layout_weight="1"
android:orientation="horizontal"
android:layout_gravity="center" />
...

我的问题是在 Activity 类中到达此行后将抛出 IndexOutOfBoundsException(正如我在上面的代码中指定的那样):

chart1.addView(mChartView);

然后应用程序将被关闭。我搜索了很多,无法弄清楚我的错误是什么?谁能帮忙?

编辑:这是我的日志猫:

07-16 09:38:26.854: E/AndroidRuntime(25648): FATAL EXCEPTION: main
07-16 09:38:26.854: E/AndroidRuntime(25648): java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0
07-16 09:38:26.854: E/AndroidRuntime(25648): at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:251)
07-16 09:38:26.854: E/AndroidRuntime(25648): at java.util.ArrayList.get(ArrayList.java:304)
07-16 09:38:26.854: E/AndroidRuntime(25648): at org.achartengine.renderer.DefaultRenderer.getSeriesRendererAt(DefaultRenderer.java:189)
07-16 09:38:26.854: E/AndroidRuntime(25648): at org.achartengine.chart.DoughnutChart.draw(DoughnutChart.java:109)
07-16 09:38:26.854: E/AndroidRuntime(25648): at org.achartengine.GraphicalView.onDraw(GraphicalView.java:168)
07-16 09:38:26.854: E/AndroidRuntime(25648): at android.view.View.draw(View.java:10978)
07-16 09:38:26.854: E/AndroidRuntime(25648): at android.view.ViewGroup.drawChild(ViewGroup.java:2887)
07-16 09:38:26.854: E/AndroidRuntime(25648): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2489)
07-16 09:38:26.854: E/AndroidRuntime(25648): at android.view.ViewGroup.drawChild(ViewGroup.java:2885)
07-16 09:38:26.854: E/AndroidRuntime(25648): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2489)
07-16 09:38:26.854: E/AndroidRuntime(25648): at android.view.ViewGroup.drawChild(ViewGroup.java:2885)
07-16 09:38:26.854: E/AndroidRuntime(25648): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2489)
07-16 09:38:26.854: E/AndroidRuntime(25648): at android.view.ViewGroup.drawChild(ViewGroup.java:2885)
07-16 09:38:26.854: E/AndroidRuntime(25648): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2489)
07-16 09:38:26.854: E/AndroidRuntime(25648): at android.view.View.draw(View.java:10981)
07-16 09:38:26.854: E/AndroidRuntime(25648): at android.widget.FrameLayout.draw(FrameLayout.java:450)
07-16 09:38:26.854: E/AndroidRuntime(25648): at android.view.ViewGroup.drawChild(ViewGroup.java:2887)
07-16 09:38:26.854: E/AndroidRuntime(25648): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2489)
07-16 09:38:26.854: E/AndroidRuntime(25648): at android.view.View.draw(View.java:10981)
07-16 09:38:26.854: E/AndroidRuntime(25648): at android.view.ViewGroup.drawChild(ViewGroup.java:2887)
07-16 09:38:26.854: E/AndroidRuntime(25648): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2489)
07-16 09:38:26.854: E/AndroidRuntime(25648): at android.view.ViewGroup.drawChild(ViewGroup.java:2885)
07-16 09:38:26.854: E/AndroidRuntime(25648): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2489)
07-16 09:38:26.854: E/AndroidRuntime(25648): at android.view.ViewGroup.drawChild(ViewGroup.java:2885)
07-16 09:38:26.854: E/AndroidRuntime(25648): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2489)
07-16 09:38:26.854: E/AndroidRuntime(25648): at android.view.View.draw(View.java:10981)
07-16 09:38:26.854: E/AndroidRuntime(25648): at android.widget.FrameLayout.draw(FrameLayout.java:450)
07-16 09:38:26.854: E/AndroidRuntime(25648): at com.android.internal.policy.impl.PhoneWindow$DecorView.draw(PhoneWindow.java:2126)
07-16 09:38:26.854: E/AndroidRuntime(25648): at android.view.ViewRootImpl.draw(ViewRootImpl.java:2026)
07-16 09:38:26.854: E/AndroidRuntime(25648): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1634)
07-16 09:38:26.854: E/AndroidRuntime(25648): at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2442)
07-16 09:38:26.854: E/AndroidRuntime(25648): at android.os.Handler.dispatchMessage(Handler.java:99)
07-16 09:38:26.854: E/AndroidRuntime(25648): at android.os.Looper.loop(Looper.java:137)
07-16 09:38:26.854: E/AndroidRuntime(25648): at android.app.ActivityThread.main(ActivityThread.java:4424)
07-16 09:38:26.854: E/AndroidRuntime(25648): at java.lang.reflect.Method.invokeNative(Native Method)
07-16 09:38:26.854: E/AndroidRuntime(25648): at java.lang.reflect.Method.invoke(Method.java:511)
07-16 09:38:26.854: E/AndroidRuntime(25648): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
07-16 09:38:26.854: E/AndroidRuntime(25648): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
07-16 09:38:26.854: E/AndroidRuntime(25648): at dalvik.system.NativeStart.main(Native Method)

提前致谢。

最佳答案

您有一个 MultipleCategorySeries,但您没有将任何 SimpleSeriesRenderer 添加到 mRenderer。参见 this举个例子。

关于android - 将图表引擎的圆环图 View 添加到线性布局时出现 IndexOutOfBoundsException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17676032/

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