这是我的应用程序代码的一部分,它在折线图上绘制特定日期的值。从 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:如果我的问题不清楚,请向我进行一些编辑。感谢你。