0

我已经设置了图表引擎,它运行良好,在尝试向图表添加另一个图表后,我在 log cat 上收到以下错误消息。代码中没有列表,尽管它是从 listactivity 扩展而来的。

代码有什么问题。

package org.achartengine.chartdemo.demo;

import java.text.Format;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.GregorianCalendar;
import org.achartengine.ChartFactory;
import org.achartengine.GraphicalView;
import org.achartengine.chart.PointStyle;
import org.achartengine.model.SeriesSelection;
import org.achartengine.model.TimeSeries;
import org.achartengine.model.XYMultipleSeriesDataset;
import org.achartengine.renderer.XYMultipleSeriesRenderer;
import org.achartengine.renderer.XYSeriesRenderer;
import android.annotation.SuppressLint;
import android.app.ListActivity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.Toast;

@SuppressLint("SimpleDateFormat")
public class ChartDemo extends ListActivity {
     private GraphicalView mChart;

        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.xy_chart);
            openChart();
        }

        private void openChart(){

            int count = 5;
            Date[] dt = new Date[5];
            for(int i=0;i<count;i++){
                GregorianCalendar gc = new GregorianCalendar(2012, 10, i+1);
                dt[i] = gc.getTime();
            }

            int[] visits = { 2000,2500,2700,2100,2800};
            int[] views = {2200, 2700, 2900, 2800, 3200};
            int[] views3 = {2100, 2740, 2100, 2900, 3100};
            // Creating TimeSeries for Visits
            TimeSeries visitsSeries = new TimeSeries("Visits");

            // Creating TimeSeries for Views
            TimeSeries viewsSeries = new TimeSeries("Views");
            TimeSeries viewsSeries3 = new TimeSeries("Views3");

            // Adding data to Visits and Views Series
            for(int i=0;i<dt.length;i++){
                visitsSeries.add(dt[i], visits[i]);
                viewsSeries.add(dt[i],views[i]);
                viewsSeries3.add(dt[i],views3[i]);
            }

            // Creating a dataset to hold each series
            XYMultipleSeriesDataset dataset = new XYMultipleSeriesDataset();

            // Adding Visits Series to the dataset
            dataset.addSeries(visitsSeries);

            // Adding Visits Series to dataset
            dataset.addSeries(viewsSeries);
            dataset.addSeries(viewsSeries3);

            // Creating XYSeriesRenderer to customize visitsSeries
            XYSeriesRenderer visitsRenderer = new XYSeriesRenderer();
            visitsRenderer.setColor(Color.WHITE);
            visitsRenderer.setPointStyle(PointStyle.CIRCLE);
            visitsRenderer.setFillPoints(true);
            visitsRenderer.setLineWidth(2);
            visitsRenderer.setDisplayChartValues(true);

            // Creating XYSeriesRenderer to customize viewsSeries
            XYSeriesRenderer viewsRenderer = new XYSeriesRenderer();
            viewsRenderer.setColor(Color.YELLOW);
            viewsRenderer.setPointStyle(PointStyle.CIRCLE);
            viewsRenderer.setFillPoints(true);
            viewsRenderer.setLineWidth(2);
            viewsRenderer.setDisplayChartValues(true);

            // Creating XYSeriesRenderer3 to customize viewsSeries
            XYSeriesRenderer viewsRenderer3 = new XYSeriesRenderer();
            viewsRenderer.setColor(Color.YELLOW);
            viewsRenderer.setPointStyle(PointStyle.CIRCLE);
            viewsRenderer.setFillPoints(true);
            viewsRenderer.setLineWidth(2);
            viewsRenderer.setDisplayChartValues(true);

            // Creating a XYMultipleSeriesRenderer to customize the whole chart
            XYMultipleSeriesRenderer multiRenderer = new XYMultipleSeriesRenderer();

            multiRenderer.setChartTitle("Visits vs Views Chart");
            multiRenderer.setXTitle("Days");
            multiRenderer.setYTitle("Count");
            multiRenderer.setZoomButtonsVisible(true);

            // Adding visitsRenderer and viewsRenderer to multipleRenderer
            // Note: The order of adding dataseries to dataset and renderers to multipleRenderer
            // should be same
            multiRenderer.addSeriesRenderer(visitsRenderer);
            multiRenderer.addSeriesRenderer(viewsRenderer);
            multiRenderer.addSeriesRenderer(viewsRenderer3);
            // Getting a reference to LinearLayout of the MainActivity Layout
            LinearLayout chartContainer = (LinearLayout) findViewById(R.id.chart_container);

            // Creating a Time Chart
            mChart = (GraphicalView) ChartFactory.getTimeChartView(getBaseContext(), dataset, multiRenderer,"dd-MMM-yyyy");

            multiRenderer.setClickEnabled(true);
            multiRenderer.setSelectableBuffer(10);

            // Setting a click event listener for the graph
            mChart.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Format formatter = new SimpleDateFormat("dd-MMM-yyyy");

                    SeriesSelection seriesSelection = mChart.getCurrentSeriesAndPoint();

                    if (seriesSelection != null) {
                        int seriesIndex = seriesSelection.getSeriesIndex();
                        String selectedSeries="Visits";
                        if(seriesIndex==0)
                            selectedSeries = "Visits";
                        else
                            selectedSeries = "Views";

                        // Getting the clicked Date ( x value )
                        long clickedDateSeconds = (long) seriesSelection.getXValue();
                        Date clickedDate = new Date(clickedDateSeconds);
                        String strDate = formatter.format(clickedDate);

                        // Getting the y value
                        int amount = (int) seriesSelection.getValue();

                        // Displaying Toast Message
                        Toast.makeText(
                            getBaseContext(),
                            selectedSeries + " on "  + strDate + " : " + amount ,
                            Toast.LENGTH_SHORT).show();
                        }
                    }
                });

                // Adding the Line Chart to the LinearLayout
                chartContainer.addView(mChart);
        }

//      @Override
//      public boolean onCreateOptionsMenu(Menu menu) {
//          getMenuInflater().inflate(R., menu);
//          return true;
//      }
}

日志猫:

  03-19 20:25:16.690: E/AndroidRuntime(753): FATAL EXCEPTION: main
03-19 20:25:16.690: E/AndroidRuntime(753): java.lang.RuntimeException: Unable to start activity ComponentInfo{org.achartengine.chartdemo.demo/org.achartengine.chartdemo.demo.ChartDemo}: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
03-19 20:25:16.690: E/AndroidRuntime(753):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
03-19 20:25:16.690: E/AndroidRuntime(753):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
03-19 20:25:16.690: E/AndroidRuntime(753):  at android.app.ActivityThread.access$1500(ActivityThread.java:117)
03-19 20:25:16.690: E/AndroidRuntime(753):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
03-19 20:25:16.690: E/AndroidRuntime(753):  at android.os.Looper.loop(Looper.java:123)
03-19 20:25:16.690: E/AndroidRuntime(753):  at android.app.ActivityThread.main(ActivityThread.java:3683)
03-19 20:25:16.690: E/AndroidRuntime(753):  at java.lang.reflect.Method.invokeNative(Native Method)
03-19 20:25:16.690: E/AndroidRuntime(753):  at java.lang.reflect.Method.invoke(Method.java:507)
03-19 20:25:16.690: E/AndroidRuntime(753):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
03-19 20:25:16.690: E/AndroidRuntime(753):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
03-19 20:25:16.690: E/AndroidRuntime(753):  at dalvik.system.NativeStart.main(Native Method)
03-19 20:25:16.690: E/AndroidRuntime(753): Caused by: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
03-19 20:25:16.690: E/AndroidRuntime(753):  at android.app.ListActivity.onContentChanged(ListActivity.java:243)
03-19 20:25:16.690: E/AndroidRuntime(753):  at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:210)
03-19 20:25:16.690: E/AndroidRuntime(753):  at android.app.Activity.setContentView(Activity.java:1657)
03-19 20:25:16.690: E/AndroidRuntime(753):  at org.achartengine.chartdemo.demo.ChartDemo.onCreate(ChartDemo.java:47)
03-19 20:25:16.690: E/AndroidRuntime(753):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
03-19 20:25:16.690: E/AndroidRuntime(753):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
03-19 20:25:16.690: E/AndroidRuntime(753):  ... 11 more

这是xml代码

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <TextView
        android:id="@+id/tv_title"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/str_tv_title"
        android:gravity="center_horizontal" />

    <LinearLayout
        android:id="@+id/chart_container"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/tv_title" >

    </LinearLayout>

</RelativeLayout>
4

1 回答 1

3

如果您使用ListActivity自定义布局,您的布局必须包含ListView带有 idandroid:id="@android:id/list"

于 2013-03-19T14:51:51.377 回答