0

这是我的应用程序代码的一部分,它在折线图上绘制特定日期的值。从 Android 的数据库中检索日期。

这是 MainActivity.java 中折线图的代码:

public void openChart() {
        // TODO Auto-generated method stub
        Date[] dt = new Date[100];//new Date[c.getCount()];
        int i=0;
        c=dbAdapter.getAllRecords();
        c.moveToFirst();
        //List<Date> dt = new ArrayList<Date>();

        while(c.moveToNext()){
            String stringDate = c.getString(1);
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date date = null;
            try {
                date = sdf.parse(stringDate);
            } catch (ParseException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            dt[i] = date;//dt.add(date);//
            i++;
        }

        //double[] numbers = {20.0,20.1,20.2,20.3,20.4,20.5,20.6,20.7,20.8,20.9,21.0,21.2};
        Double[] numbers = new Double[100];

        TimeSeries numbersTime = new TimeSeries("Numbers Time");

        for(int i2=0;i2<dt.length;i2++){
            numbers[i2]=(double) i2;
            numbersTime.add(dt[i2],numbers[i2]);
        }

        XYMultipleSeriesDataset dataset = new XYMultipleSeriesDataset();
        dataset.addSeries(numbersTime);


        XYSeriesRenderer numbersRenderer = new XYSeriesRenderer();
        numbersRenderer.setColor(Color.BLACK);
        numbersRenderer.setPointStyle(PointStyle.CIRCLE);
        numbersRenderer.setFillPoints(true);
        numbersRenderer.setLineWidth(2);
        numbersRenderer.setDisplayChartValues(true);

        XYMultipleSeriesRenderer multiRenderer = new XYMultipleSeriesRenderer();

         multiRenderer.setChartTitle("Visits vs Views Chart");
         multiRenderer.setXTitle("Date Time");
         multiRenderer.setYTitle("Double Numbers");
         multiRenderer.setZoomButtonsVisible(true);

         multiRenderer.addSeriesRenderer(numbersRenderer);

         // Creating an intent to plot line chart using dataset and multipleRenderer
            Intent intent = ChartFactory.getLineChartIntent(getBaseContext(), dataset, multiRenderer);


            // Start Activity
            startActivity(intent);
    }

这里是 LogCat 文件行:

01-25 19:14:30.531: D/AndroidRuntime(31866): Shutting down VM
01-25 19:14:30.531: W/dalvikvm(31866): threadid=1: thread exiting with uncaught exception (group=0x40018578)
01-25 19:14:30.546: E/AndroidRuntime(31866): FATAL EXCEPTION: main
01-25 19:14:30.546: E/AndroidRuntime(31866): java.lang.NullPointerException
01-25 19:14:30.546: E/AndroidRuntime(31866):    at org.achartengine.model.TimeSeries.add(TimeSeries.java:41)
01-25 19:14:30.546: E/AndroidRuntime(31866):    at com.example.dateexample.seenu.MainActivity.openChart(MainActivity.java:105)
01-25 19:14:30.546: E/AndroidRuntime(31866):    at com.example.dateexample.seenu.MainActivity$2.onClick(MainActivity.java:70)
01-25 19:14:30.546: E/AndroidRuntime(31866):    at android.view.View.performClick(View.java:2485)
01-25 19:14:30.546: E/AndroidRuntime(31866):    at android.view.View$PerformClick.run(View.java:9080)
01-25 19:14:30.546: E/AndroidRuntime(31866):    at android.os.Handler.handleCallback(Handler.java:587)
01-25 19:14:30.546: E/AndroidRuntime(31866):    at android.os.Handler.dispatchMessage(Handler.java:92)
01-25 19:14:30.546: E/AndroidRuntime(31866):    at android.os.Looper.loop(Looper.java:130)
01-25 19:14:30.546: E/AndroidRuntime(31866):    at android.app.ActivityThread.main(ActivityThread.java:3687)
01-25 19:14:30.546: E/AndroidRuntime(31866):    at java.lang.reflect.Method.invokeNative(Native Method)
01-25 19:14:30.546: E/AndroidRuntime(31866):    at java.lang.reflect.Method.invoke(Method.java:507)
01-25 19:14:30.546: E/AndroidRuntime(31866):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
01-25 19:14:30.546: E/AndroidRuntime(31866):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
01-25 19:14:30.546: E/AndroidRuntime(31866):    at dalvik.system.NativeStart.main(Native Method)

在这里,我在第 105 行遇到了 nullPointer 异常,它是 'numbersTime.add(dt[i2],numbers[i2]);' 陈述。我不清楚如何从数据库中检索日期并将它们绘制在聊天中。请为我的异常提供解决方案。

PS:如果我的问题不清楚,请向我进行一些编辑。感谢你。

4

2 回答 2

1

看起来您dt[i2]在某些时候为空,这可能是由您的date = sdf.parse(stringDate);. 如果解析失败,则date不会设置,因此它将保持为空。

尝试将其设置为您的 catch 中的默认值,或在执行之前专门检查 nullnumbersTime.add(dt[i2],numbers[i2])

于 2013-01-25T14:07:31.757 回答
0

在我的项目中,我有某事。像这样:

List<Date[]> dates = new ArrayList<Date[]>();
dates.add(new Date[valueY.length]);

for(int i = 0; i < valueY.length; i++) {
     dates.get(0)[i] = new Date((Integer.valueOf(valueY[i].split("/")[2]) - 1900), (parseDayAndMonth(valueY[i].split("/")[1]) - 1), parseDayAndMonth(valueY[i].split("/")[0]));
}

valueYString数组。

String valueY = new String[]{"24/10/2013", "12/07/2013"};

我得到这样的图表:

ChartFactory.getTimeChartView(ReportsEngine.getApplicationContext(), buildDateDataset(titles, dates, values), renderer, "dd/MM");

这是我的parseDayAndMonth()方法。

public static Integer parseDayAndMonth(String number) {
    Integer num = Integer.valueOf(number);
    if(num < 10) {
        number = number.replace("0", "");
    }
    return Integer.valueOf(number);
}
于 2013-01-25T14:03:39.247 回答