2

我正在尝试从获取 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>
4

1 回答 1

3

我只是遇到了类似的问题,解决方案正如@developeralways所说,使用XYSeriesRenderer而不是SimpleSeriesRenderer,虽然我使用SimpleSeriesRenderer时没有编译错误,并且在我更新到版本1.2.0之后它之前工作(achartengine 1.0.0) ,它不再起作用了。

renderer = new XYMultipleSeriesRenderer();
    SimpleSeriesRenderer renderBar = new SimpleSeriesRenderer();
    renderBar.setColor(StandardColor.BLUE);
    renderer.addSeriesRenderer(renderBar);

改成:

renderer = new XYMultipleSeriesRenderer();
    SimpleSeriesRenderer renderBar = new XYSeriesRenderer();
    renderBar.setColor(StandardColor.BLUE);
    renderer.addSeriesRenderer(renderBar);
于 2015-05-02T14:17:47.283 回答