从SQLite创建以日期为X轴的GraphView

时间:2018-08-21 13:10:56

标签: java android sqlite nullpointerexception android-graphview

我正在尝试根据我的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 ComponentInfo{example.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();
        }
    });
}


}

有人可以帮助我吗?

2 个答案:

答案 0 :(得分:0)

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

data.moveToFirst();

因此,游标数据指向其第一行。
然后在for循环的第一行中执行:

data.moveToNext();

因此,游标数据指向其第二行(如果存在第二行)。
因此,您肯定会错过第一行的数据。
但是由于迭代次数等于行数,所以最后一次迭代将获取空数据!
建议将data.moveToNext();作为for循环的最后一条语句。

答案 1 :(得分:0)

  

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

意味着您正在调用/正在使用的代码正在对getTime()的{​​{1}}对象调用Date

更多信息

  

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

表示此操作是在null的第48行上发起的。

上面仅存在一个VIewGraph.java块来捕获解析日期时出现的问题,但您不必检查try-catch对象最后是否为空。任何错误都将被忽略。然后,您将此空Date对象提供给Date

中的series

一种快速解决方案是更改

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

.. to ...

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

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