如何在Android中的折线图上绘制从数据库中检索到的数据

时间:2013-01-25 12:33:51

标签: android database datetime achartengine

我正在开发一个简单的Android应用程序,它将当前(现在)的日期存储到sqlite数据库中。 稍后,它会将数据库中的日期值检索到活动,并在折线图上绘制它们,并将一些值分配给日期。 我已完成所有编码,例如从数据库中检索已保存的日期并将它们存储到ArrayList,编码折线图和所有内容。最后我得到一个nullpointer异常。请仔细看看我的代码并提供解决方案。在程序中找到异常将是一项简单的任务。

这是布局xml代码:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/LinearLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Add Date" 
            android:layout_weight="1"/>

        <Button
            android:id="@+id/chartButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Chart" 
            android:layout_weight="1"/>
    </LinearLayout>

    <ListView
        android:id="@+id/listView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
    </ListView>

</LinearLayout>

chartButton用于显示使用意图的图表。

以下是折线图代码的一部分:

public void openChart() {
        // TODO Auto-generated method stub
        //Date[] dt = 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.add(date);//dt[i] = date;
            //i++;
        }

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

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

        for(int i2=0;i2<dt.size();i2++){
            numbers[i2]=(double) i2;
            numbersTime.add(dt.get(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);
    }

这是DBAdapter类中的Cursor方法:

Cursor getAllRecords(){
        String[] columns = {COL_ID,COL_DATE};
        return db.query(TABLE_NAME, columns, null, null, null, null, null); 
    }

这是清单文件:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.dateexample.seenu"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="4"
        android:targetSdkVersion="16" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.dateexample.seenu.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" />
    </application>

</manifest>

以下是LogCat报告:

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

最后我在logCat中显示null pointerException。 我再一次请求为我提供一些解决方案。

1 个答案:

答案 0 :(得分:2)

您正在尝试向其中添加值的单元化数组(Double[] numbers = null;)。您需要在为其添加值之前分配numbers数组。

Double[] numbers = new Double[100];

注意:您可能希望查看更灵活的存储容器(Vectors,ArrayList等),这些容器会随着您添加它们而增长。