2

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

活动:

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" />
...

我的问题是在活动类中到达此行后将引发 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)

提前感谢很多。

4

1 回答 1

2

你有一个MultipleCategorySeries,但你没有任何SimpleSeriesRenderer添加到mRenderer. 请参阅示例。

于 2013-07-16T13:37:21.903 回答