我正在尝试从获取 Classcastexception 的以下链接运行 achartengine 示例。无法找到问题的根源。我添加了 mainactivity.java 和 manifest.xml。任何帮助表示赞赏。
08-09 04:08:10.370: E/AndroidRuntime(3350): FATAL EXCEPTION: main
08-09 04:08:10.370: E/AndroidRuntime(3350): java.lang.ClassCastException: org.achartengine.renderer.SimpleSeriesRenderer cannot be cast to org.achartengine.renderer.XYSeriesRenderer
Mainactivity.java
public class MainActivity extends Activity {
private static final int SERIES_NR = 2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
XYMultipleSeriesRenderer renderer = getTruitonBarRenderer();
myChartSettings(renderer);
Intent intent = ChartFactory.getBarChartIntent(this,
getTruitonBarDataset(), renderer, Type.DEFAULT);
startActivity(intent);
}
private XYMultipleSeriesDataset getTruitonBarDataset() {
XYMultipleSeriesDataset dataset = new XYMultipleSeriesDataset();
final int nr = 4;
Random r = new Random();
ArrayList<String> legendTitles = new ArrayList<String>();
legendTitles.add("Sales");
legendTitles.add("Expenses");
for (int i = 0; i < SERIES_NR; i++) {
CategorySeries series = new CategorySeries(legendTitles.get(i));
for (int k = 0; k < nr; k++) {
series.add(100 + r.nextInt() % 100);
}
dataset.addSeries(series.toXYSeries());
}
return dataset;
}
public XYMultipleSeriesRenderer getTruitonBarRenderer() {
XYMultipleSeriesRenderer renderer = new XYMultipleSeriesRenderer();
renderer.setAxisTitleTextSize(16);
renderer.setChartTitleTextSize(20);
renderer.setLabelsTextSize(15);
renderer.setLegendTextSize(15);
renderer.setMargins(new int[] { 30, 40, 15, 0 });
SimpleSeriesRenderer r = new SimpleSeriesRenderer();
r.setColor(Color.BLUE);
renderer.addSeriesRenderer(r);
r = new SimpleSeriesRenderer();
r.setColor(Color.RED);
renderer.addSeriesRenderer(r);
return renderer;
}
private void myChartSettings(XYMultipleSeriesRenderer renderer) {
renderer.setChartTitle("Truiton's Performance by AChartEngine BarChart");
renderer.setXAxisMin(0.5);
renderer.setXAxisMax(10.5);
renderer.setYAxisMin(0);
renderer.setYAxisMax(210);
renderer.addXTextLabel(1, "2010");
renderer.addXTextLabel(2, "2011");
renderer.addXTextLabel(3, "2012");
renderer.addXTextLabel(4, "2013");
renderer.setYLabelsAlign(Align.RIGHT);
renderer.setBarSpacing(0.5);
renderer.setXTitle("Years");
renderer.setYTitle("Performance");
renderer.setShowGrid(true);
renderer.setGridColor(Color.GRAY);
renderer.setXLabels(0);
}
}
清单.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.piegraphicsview"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.piegraphicsview.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="org.achartengine.GraphicalActivity" >
</activity>
</application>
</manifest>