从 SQLite 创建一个以日期为 X 轴的 GraphView [重复]

Posted

技术标签:

【中文标题】从 SQLite 创建一个以日期为 X 轴的 GraphView [重复]【英文标题】:Creating a GraphView with date as X axis from SQLite [duplicate] 【发布时间】:2019-01-27 16:32:26 【问题描述】:

我正在尝试从我的 sqlite 值创建一个图表,例如这里的日期与重量。稍后我将添加 Date vs Fat 等。但是应用程序通过这些 Logcat 被手机强制关闭:

08-19 21:58:50.313 25858-25858/example.christopher.bd E/androidRuntime: FATAL EXCEPTION: main Process: example.christopher.bd, PID: 25858 java.lang.RuntimeException: Unable to start activity ComponentInfoexample.christopher.bd/example.christopher.bd.VIewGraph: java.lang.NullPointerException: Attempt to invoke virtual method 'long java.util.Date.getTime()' on a null object reference at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2913) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048) at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78) at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108) at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1808) at android.os.Handler.dispatchMessage(Handler.java:106) at android.os.Looper.loop(Looper.java:193) at android.app.ActivityThread.main(ActivityThread.java:6669) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858) Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'long java.util.Date.getTime()' on a null object reference at com.jjoe64.graphview.series.DataPoint.(DataPoint.java:45) at example.christopher.bd.VIewGraph.onCreate(VIewGraph.java:48) at android.app.Activity.performCreate(Activity.java:7136) at android.app.Activity.performCreate(Activity.java:7127) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1271) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2893) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048) at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78) at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108) at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1808) at android.os.Handler.dispatchMessage(Handler.java:106) at android.os.Looper.loop(Looper.java:193) at android.app.ActivityThread.main(ActivityThread.java:6669) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)

显示图表的活动:

package example.christopher.bd;

import android.database.Cursor;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

import com.jjoe64.graphview.GraphView;
import com.jjoe64.graphview.helper.DateAsXAxisLabelFormatter;
import com.jjoe64.graphview.series.DataPoint;
import com.jjoe64.graphview.series.LineGraphSeries;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class VIewGraph extends AppCompatActivity 

    LineGraphSeries<DataPoint> series;
    DatabaseHelper mDatabaseHelper;
    @Override
    protected void onCreate(Bundle savedInstanceState) 
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_view_graph);
        mDatabaseHelper = new DatabaseHelper(this);

        String y;
        Float z;
        Date d1;
        Cursor data = mDatabaseHelper.readEntry();
        int rows = data.getCount();
        data.moveToFirst();

        GraphView graph = (GraphView) findViewById(R.id.graph11);
        series = new LineGraphSeries<DataPoint>();

        for(int i = 0;  i <rows; i++)
            data.moveToNext();
            String x = data.getString(2);
            y = data.getString(3);
            z = Float.parseFloat(y);
            Date date1 = null;
            try 
                date1 = new SimpleDateFormat("dd/MM/yyyy").parse(x);
             catch (Exception e) 
                e.printStackTrace();
            

            series.appendData(new DataPoint(date1, z), true, 25);
        
        graph.addSeries(series);

        graph.getGridLabelRenderer().setNumHorizontalLabels(3);
        graph.getGridLabelRenderer().setHumanRounding(false);

    

这是databasehelper的代码

package example.christopher.bd;


import android.app.DatePickerDialog;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.icu.util.Calendar;
import android.util.Log;
import android.view.View;
import android.widget.TextView;


public class DatabaseHelper extends SQLiteOpenHelper

private static final String TAG = "DatabaseHelper";

private static final String TABLE_NAME = "BodyData";
private static final String COL1 = "ID";
private static final String COL2 = "tdate";
private static final String COL2a = "ttime";
private static final String COL3 = "weight";
private static final String COL4 = "fat";
private static final String COL5 = "hydration";
private static final String COL6 = "muscle";
private static final String COL7 = "bone";
//private static final String COL8 = "time";

private TextView mDisplayDate;
private DatePickerDialog.OnDateSetListener mDateSetListener;

public DatabaseHelper(Context context) 
    super(context, TABLE_NAME, null, 1);


@Override
public void onCreate(SQLiteDatabase db) 
    String createTable = " CREATE TABLE " + TABLE_NAME + " (ID INTEGER PRIMARY KEY AUTOINCREMENT, tdate string, ttime string, weight float, fat float, hydration float, muscle float, bone float)";
    //String createTable = " CREATE TABLE " + TABLE_NAME + " (ID INTEGER PRIMARY KEY AUTOINCREMENT, tday integer, tmonth integer, tyear integer, weight float, fat float, hydration float, muscle float, bone float)";
    db.execSQL(createTable);


@Override
public void onUpgrade(SQLiteDatabase db, int i, int i1) 
    db.execSQL(" DROP TABLE IF EXISTS " + TABLE_NAME);
    onCreate(db);

public boolean addData(String tdate, String ttime, String weight, String fat, String hydration, String muscle, String bone) 
    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues contentValues = new ContentValues();
    contentValues.put(COL2, tdate);
    contentValues.put(COL2a, ttime);
    contentValues.put(COL3, weight);
    contentValues.put(COL4, fat);
    contentValues.put(COL5, hydration);
    contentValues.put(COL6, muscle);
    contentValues.put(COL7, bone);
    //contentValues.put(COL8, time);
    long result = db.insert(TABLE_NAME,null ,contentValues);
    if(result == -1)
        return false;
    else
        return true;



public Cursor getData()
    SQLiteDatabase db = this.getWritableDatabase();
    String query = "SELECT * FROM " + TABLE_NAME;
    Cursor data = db.rawQuery(query, null);
    return data;


public void deleteAll()

    SQLiteDatabase db = this.getWritableDatabase();
    db.delete(TABLE_NAME,null,null);
    db.execSQL("delete from "+ TABLE_NAME);
    db.close();


public Cursor readEntry()
    SQLiteDatabase db = this.getWritableDatabase();

    String[] allColumns = new String[]
            DatabaseHelper.COL1,
            DatabaseHelper.COL2,
            DatabaseHelper.COL2a,
            DatabaseHelper.COL3,
            DatabaseHelper.COL4,
            DatabaseHelper.COL5,
            DatabaseHelper.COL6,
            DatabaseHelper.COL7,
    ;

    Cursor c = db.query(DatabaseHelper.TABLE_NAME, allColumns, null, null, null, null, null);
    if (c != null) 
        c.moveToFirst();
    
    return c;


记录数据的活动(日期、体重等)

package example.christopher.bd;

import android.app.DatePickerDialog;
import android.app.TimePickerDialog;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.icu.util.Calendar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.TimePicker;
import android.widget.Toast;

public class Logging extends AppCompatActivity 

private static final String TAG = "Logging";

DatabaseHelper mDatabaseHelper;
EditText editWeight, editFat, editHydration, editMuscle, editBone, editTime, editASD;
private Button button2;
private TextView mDisplayDate, mDisplayTime;
private DatePickerDialog.OnDateSetListener mDateSetListener;
private TimePickerDialog.OnTimeSetListener mTimeSetListener;

@Override
protected void onCreate(Bundle savedInstanceState) 
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_logging);
    button2 = (Button) findViewById(R.id.button2);
    mDisplayDate = (TextView) findViewById(R.id.tvDate);
    mDatabaseHelper = new DatabaseHelper(this);
    mDisplayTime = (TextView) findViewById(R.id.tvTime);

    editWeight = (EditText) findViewById(R.id.editWeight);
    editFat = (EditText) findViewById(R.id.editFat);
    editHydration = (EditText) findViewById(R.id.editHydration);
    editMuscle = (EditText) findViewById(R.id.editMuscle);
    editBone = (EditText) findViewById(R.id.editBone);

    mDisplayDate.setOnClickListener(new View.OnClickListener() 
        @Override
        public void onClick(View view) 
            Calendar cal = Calendar.getInstance();
            int year = cal.get(Calendar.YEAR);
            int month = cal.get(Calendar.MONTH);
            int day = cal.get(Calendar.DAY_OF_MONTH);

            DatePickerDialog dialog = new DatePickerDialog(
                    Logging.this,
                    android.R.style.Theme_Holo_Light_Dialog_MinWidth,
                    mDateSetListener,
                    year,month,day);
            dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
            dialog.show();

        
    );

    mDateSetListener = new DatePickerDialog.OnDateSetListener() 
        @Override
        public void onDateSet(DatePicker datePicker, int year, int month, int day) 
            month = month + 1;
            Log.d(TAG, "onDateSet: mm/dd/yyyy: " + day + "/" + month + "/" + year);

            String date = day + "/" + month + "/" + year;
            mDisplayDate.setText(date);
        
    ;

    mDisplayTime.setOnClickListener(new View.OnClickListener() 
                                        @Override
                                        public void onClick(View view) 
                                            Calendar mTime = Calendar.getInstance();
                                            int mHour = mTime.get(Calendar.HOUR_OF_DAY);
                                            int mMinute = mTime.get(Calendar.MINUTE);

                                            TimePickerDialog timePickerDialog = new TimePickerDialog(Logging.this,
                                                    new TimePickerDialog.OnTimeSetListener() 

                                                        @Override
                                                        public void onTimeSet(TimePicker view, int hourOfDay,
                                                                              int minute) 

                                                            mDisplayTime.setText(hourOfDay + ":" + minute);
                                                        
                                                    , mHour, mMinute, true);
                                            timePickerDialog.show();
                                        
                                    );

    LogData();


public void LogData()
    button2.setOnClickListener(new View.OnClickListener() 
        @Override
        public void onClick(View v) 
            boolean isInserted = mDatabaseHelper.addData(
                    mDisplayDate.getText().toString(),
                    mDisplayTime.getText().toString(),
                    editWeight.getText().toString(),
                    editFat.getText().toString(),
                    editHydration.getText().toString(),
                    editMuscle.getText().toString(),
                    editBone.getText().toString()
            );
            if (isInserted == true)
                Toast.makeText(Logging.this, "Data Inserted", Toast.LENGTH_LONG).show();
            else
                Toast.makeText(Logging.this, "Data not Inserted", Toast.LENGTH_LONG).show();
        
    );




谁能帮帮我?

【问题讨论】:

【参考方案1】:

java.lang.NullPointerException: 尝试调用虚方法 'long 空对象引用上的 java.util.Date.getTime()'

表示您正在调用/您正在使用的代码正在调用 Date 对象上的 getTime(),该对象是 null

更详细的

尝试调用虚拟方法“long java.util.Date.getTime()” 空对象引用 com.jjoe64.graphview.series.DataPoint.(DataPoint.java:45) 在 example.christopher.bd.VIewGraph.onCreate(VIewGraph.java:48)

表示这是在第 48 行的VIewGraph.java 中发起的。

就在上面有一个try-catch 块来捕获解析日期的问题,但你没有检查Date 对象最后是否非空。任何错误都会被忽略。然后你将这个空的Date 对象提供给series.appendData(new DataPoint(date1, z), true, 25); 中的series

快速解决办法是改变

try 
    date1 = new SimpleDateFormat("dd/MM/yyyy").parse(x);
 catch (Exception e) 
    e.printStackTrace();


series.appendData(new DataPoint(date1, z), true, 25);

..到...

try 
    date1 = new SimpleDateFormat("dd/MM/yyyy").parse(x);
    series.appendData(new DataPoint(date1, z), true, 25);
 catch (Exception e) 
    e.printStackTrace();

【讨论】:

根据您的建议和 mTak 的建议,我的应用程序现在没有崩溃,但 graphview 没有显示任何点或线。 X 和 Y 轴上只有 0 到 1 的网格。这是 logcat 08-21 22:10:11.798 5545-5557/example.christopher.bd W/SQLiteConnectionPool:数据库“/data/user/0/example.christopher.bd/databases/BodyData”的 SQLiteConnection 对象被泄露!请修复您的应用程序以正确结束正在进行的事务并在不再需要时关闭数据库。 08-21 22:10:11.798 5545-5557/example.christopher.bd W/System: 资源调用关闭失败。 好吧,这至少是一些进步。当然,您应该检查(也许调试)日期转换是否有效。如果那里确实存在错误,那么您将永远无法将任何数据添加到图表中。泄漏的连接可能更多的是警告而不是严重错误,但当然您应该关闭不再需要的任何资源。我自己最近没有玩过 SQLite,所以不再对此发表评论。 如何检查日期转换是否有效? 当然有debugging 和logging。您可以设置断点或将日志添加到代码的有趣分支,并在必要时为断点或日志添加临时代码块。断点对于意外情况很有意义,例如当代码进入 catch 块时,日志记录可能有助于证明代码按预期工作。如果您在 date1 = new SimpleDateFormat("dd/MM/yyyy").parse(x); 之后有日志,则只有在没有异常的情况下才会打印出来。【参考方案2】:

您似乎对表格的列进行了更改(COL2a 而不是COL8) 如果是这种情况,您是否从测试它的设备/模拟器中卸载了该应用程序? 如果不是,你必须。 每次在 onCreate()DatabaseHelper 类中进行更改时,表架构都不会更改。 onCreate() 在 db 不存在时执行。 现在是一个逻辑问题: 在您的活动的 onCreate() 中,您执行:

data.moveToFirst();

所以游标数据指向它的第一行。 然后在 for 循环的第一行执行:

data.moveToNext();

所以游标数据指向它的第 2 行(如果有第 2 行)。 所以你肯定会错过第一行的数据。 但由于迭代次数与行数相等,最后一次迭代将获取空数据! 建议将data.moveToNext(); 作为for 循环的最后一条语句。

【讨论】:

您的建议已实施。现在,我记录了 4 行数据,但 GraphView 中没有显示任何内容,只有 X 轴和 Y 轴上的 0 到 1 网格。这是 logcat 08-21 22:10:11.798 5545-5557/example.christopher.bd W/SQLiteConnectionPool:数据库“/data/user/0/example.christopher.bd/databases/BodyData”的 SQLiteConnection 对象被泄露!请修复您的应用程序以正确结束正在进行的事务并在不再需要时关闭数据库。 08-21 22:10:11.798 5545-5557/example.christopher.bd W/System: 资源调用关闭失败。 @ChristopherGo 你的问题是关于java.lang.NullPointerException。现在这个问题解决了。您必须为新问题发布另一个问题,提供所有必要的代码和数据。

以上是关于从 SQLite 创建一个以日期为 X 轴的 GraphView [重复]的主要内容,如果未能解决你的问题,请参考以下文章

带有日期时间轴的 Seaborn 热图

如何在 Plotly 中添加跟踪,并为沿 x 轴的每个数据点设置自定义顺序?

X轴的Google Live图形值按降序排列

SQLite组日期时间以168小时(7天)为单位返回NULL

R语言将字符串按照一定的格式(format)转化为日期格式ggplot2可视化水平分面图(faceting)自定义X轴的时间标签旋转

R语言ggplot2可视化强制指定X轴的日期范围实战:组合条形图和lm模型构建的回归模型的结果并强制指定X轴的日期范围