我想在我的活动中展示圆环图。事实上,我的活动包括一些片段。因此它不会扩展Activity,但会扩展FragmentActivity。 我正在使用achartengine的doughnutchartview通过将圆环图添加到线性布局中来将活动中的圆环图(不在其片段内)中集成。线性布局已在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 layout:
<?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)
Thanx很多事先。