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



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

下面是布局 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)

最后,我在日志猫中显示空指针异常。我再次请求为我提供一些解决方案。

您有一个单位化数组 ( Double[] numbers = null; ),您正在尝试向其添加值。 您需要先分配numbers数组,然后再向其添加值。

Double[] numbers = new Double[100];

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

相关内容

  • 没有找到相关文章

最新更新