我正在创建一个收集多个(android)移动传感器数据的应用程序。我的应用程序工作正常,但当我尝试将数据保存到sqlite表时,应用程序崩溃并停止工作。我为每个传感器和不同的插入函数创建了表,每个函数都应在不同的线程中调用(每个传感器一个线程)。如何以最有效的方式存储传感器数据
这是我的代码。
public class SenosorsDBAdapter {
private SensorsDBHelper myhelper;
public SenosorsDBAdapter(Context context){ myhelper = new SensorsDBHelper(context); }
public long Accelerometerinsert(int ST,String sensorname, String date,float x,float y,float z) {
long id=0;
SQLiteDatabase db = myhelper.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(SensorsDBHelper.CREATION_DATE, date);
contentValues.put(SensorsDBHelper.SENSOR_NAME, sensorname);
contentValues.put(SensorsDBHelper.V_0, x);
contentValues.put(SensorsDBHelper.V_1, y);
contentValues.put(SensorsDBHelper.V_2, z);
if (ST == Sensor.TYPE_ACCELEROMETER) {
id = db.insert(SensorsDBHelper.TABLE_ACCELEROMETER, null, contentValues);
Log.i("insertData", "ACCELEROMETER insertData successfully ========================================>");
db.close();
}
return id;
}
public long Gyroscopeinsert(int ST,String sensorname, String date,float x,float y,float z) {
long id=0;
SQLiteDatabase db = myhelper.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(SensorsDBHelper.CREATION_DATE, date);
contentValues.put(SensorsDBHelper.SENSOR_NAME, sensorname);
contentValues.put(SensorsDBHelper.V_0, x);
contentValues.put(SensorsDBHelper.V_1, y);
contentValues.put(SensorsDBHelper.V_2, z);
Log.i("Gyroscopeinsert", ST+" ========================================>");
if (ST == Sensor.TYPE_GYROSCOPE) {
id = db.insert(SensorsDBHelper.TABLE_GYROSCOPE, null, contentValues);
Log.i("insertData", "Gyroscope insertData successfully ========================================>");
db.close();
}
return id;
}
public long LinearAccelerationinsert(int ST,String sensorname, String date,float x,float y,float z) {
long id=0;
SQLiteDatabase db = myhelper.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(SensorsDBHelper.CREATION_DATE, date);
contentValues.put(SensorsDBHelper.SENSOR_NAME, sensorname);
contentValues.put(SensorsDBHelper.V_0, x);
contentValues.put(SensorsDBHelper.V_1, y);
contentValues.put(SensorsDBHelper.V_2, z);
Log.i("LinearAcceinsert", ST+" ========================================>");
if (ST == Sensor.TYPE_LINEAR_ACCELERATION) {
id = db.insert(SensorsDBHelper.TABLE_LinearAcceleration, null, contentValues);
Log.i("insertData", "LinearAcceleration insertData successfully ========================================>");
db.close();
}
return id;
}
public long RotationVectorinsert(int ST,String sensorname, String date,float x,float y,float z) {
long id=0;
SQLiteDatabase db = myhelper.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(SensorsDBHelper.CREATION_DATE, date);
contentValues.put(SensorsDBHelper.SENSOR_NAME, sensorname);
contentValues.put(SensorsDBHelper.V_0, x);
contentValues.put(SensorsDBHelper.V_1, y);
contentValues.put(SensorsDBHelper.V_2, z);
if (ST == Sensor.TYPE_ROTATION_VECTOR) {
id = db.insert(SensorsDBHelper.TABLE_RotationVector, null, contentValues);
db.close();
}
return id;
}
public long Magnetometerinsert(int ST,String sensorname, String date,float x,float y,float z) {
long id=0;
SQLiteDatabase db = myhelper.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(SensorsDBHelper.CREATION_DATE, date);
contentValues.put(SensorsDBHelper.SENSOR_NAME, sensorname);
contentValues.put(SensorsDBHelper.V_0, x);
contentValues.put(SensorsDBHelper.V_1, y);
contentValues.put(SensorsDBHelper.V_2, z);
if (ST == Sensor.TYPE_MAGNETIC_FIELD) {
id = db.insert(SensorsDBHelper.TABLE_Magnetometer, null, contentValues);
db.close();
}
return id;
}
public long Orientationinsert(int ST,String sensorname, String date,float x,float y,float z) {
long id=0;
SQLiteDatabase db = myhelper.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(SensorsDBHelper.CREATION_DATE, date);
contentValues.put(SensorsDBHelper.SENSOR_NAME, sensorname);
contentValues.put(SensorsDBHelper.V_0, x);
contentValues.put(SensorsDBHelper.V_1, y);
contentValues.put(SensorsDBHelper.V_2, z);
if (ST == Sensor.TYPE_ORIENTATION) {
id = db.insert(SensorsDBHelper.TABLE_Orientation, null, contentValues);
db.close();
}
return id;
}
static class SensorsDBHelper extends SQLiteOpenHelper {
private static final String DATABASE_NAME = "sensorsDatabase.db";
private static final String TABLE_ACCELEROMETER = "ACCELEROMETER_Table";
private static final String TABLE_GYROSCOPE = "GYROSCOPE_sensorsTable";
private static final String TABLE_LinearAcceleration = "LinearAcceleration_sensorsTable";
private static final String TABLE_RotationVector = "RotationVector_sensorsTable";
private static final String TABLE_Magnetometer = "Magnetometer_sensorsTable";
private static final String TABLE_Orientation = "RotationOrientation_sensorsTable";
private static final int DATABASE_Version = 1; // Database Version
private static final String UID = "id"; // Column I (Primary Key)
private static final String CREATION_DATE = "creationdate";// Column II
private static final String SENSOR_NAME = "sensorname";// Column III
private static final String V_0 = "firstvalue"; // Column IV
private static final String V_1 = "secondvalue"; // Column V
private static final String V_2 = "thirdvalue"; // Column VI
private static final String CREATE_TABLE_ACCELEROMETER = "CREATE TABLE IF NOT EXISTS " + TABLE_ACCELEROMETER + " (" + UID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + CREATION_DATE + " TEXT," + SENSOR_NAME + " TEXT," + V_0 + " REAL," + V_1 + " REAL," + V_2 + " REAL " + ")";
private static final String CREATE_TABLE_GYROSCOPE = "CREATE TABLE IF NOT EXISTS " + TABLE_GYROSCOPE + " (" + UID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + CREATION_DATE + " TEXT," + SENSOR_NAME + " TEXT," + V_0 + " REAL," + V_1 + " REAL," + V_2 + " REAL " + ")";
private static final String CREATE_TABLE_LinearAcceleration = "CREATE TABLE IF NOT EXISTS " + TABLE_LinearAcceleration + " (" + UID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + CREATION_DATE + " TEXT," + SENSOR_NAME + " TEXT," + V_0 + " REAL," + V_1 + " REAL," + V_2 + " REAL " + ")";
private static final String CREATE_TABLE_RotationVector = "CREATE TABLE IF NOT EXISTS " + TABLE_RotationVector + " (" + UID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + CREATION_DATE + " TEXT," + SENSOR_NAME + " TEXT," + V_0 + " REAL," + V_1 + " REAL," + V_2 + " REAL " + ")";
private static final String CREATE_TABLE_Magnetometer = "CREATE TABLE IF NOT EXISTS " + TABLE_Magnetometer + " (" + UID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + CREATION_DATE + " TEXT," + SENSOR_NAME + " TEXT," + V_0 + " REAL," + V_1 + " REAL," + V_2 + " REAL " + ")";
private static final String CREATE_TABLE_Orientation = "CREATE TABLE IF NOT EXISTS " + TABLE_Orientation + " (" + UID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + CREATION_DATE + " TEXT," + SENSOR_NAME + " TEXT," + V_0 + " REAL," + V_1 + " REAL," + V_2 + " REAL " + ")";
private static final String DROP_TABLE_ACCELEROMETER = "DROP TABLE IF EXISTS " + TABLE_ACCELEROMETER;
private static final String DROP_TABLE_GYROSCOPE = "DROP TABLE IF EXISTS " + TABLE_GYROSCOPE;
private static final String DROP_TABLE_LinearAcceleration = "DROP TABLE IF EXISTS " + TABLE_LinearAcceleration;
private static final String DROP_TABLE_RotationVector = "DROP TABLE IF EXISTS " + TABLE_RotationVector;
private static final String DROP_TABLE_Magnetometer = "DROP TABLE IF EXISTS " + TABLE_Magnetometer;
private static final String DROP_TABLE_Orientation = "DROP TABLE IF EXISTS " + TABLE_Orientation;
private Context context;
public SensorsDBHelper(Context context) {
super(context, context.getExternalFilesDir(null).getAbsolutePath() + "/" + DATABASE_NAME, null, DATABASE_Version);
this.context = context;
}
public void onCreate(SQLiteDatabase db) {
try {
db.execSQL(CREATE_TABLE_ACCELEROMETER);
db.execSQL(CREATE_TABLE_GYROSCOPE);
db.execSQL(CREATE_TABLE_LinearAcceleration);
db.execSQL(CREATE_TABLE_RotationVector);
db.execSQL(CREATE_TABLE_Magnetometer);
db.execSQL(CREATE_TABLE_Orientation);
} catch (Exception e) {
Log.i("Exception", e+"========================================>");
}
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
try {
db.execSQL(DROP_TABLE_ACCELEROMETER);
db.execSQL(DROP_TABLE_GYROSCOPE);
db.execSQL(DROP_TABLE_LinearAcceleration);
db.execSQL(DROP_TABLE_RotationVector);
db.execSQL(DROP_TABLE_Magnetometer);
db.execSQL(DROP_TABLE_Orientation);
onCreate(db);
} catch (Exception e) {
Log.i("Exception", e+"========================================>");
}
}
}}
Logcat是
05-14 08:16:27.035 15796-15796/com.ttt.ddd.sdc_v2 E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.ttt.ddd.sdc_v2, PID: 15796
java.lang.NullPointerException: Attempt to invoke virtual method 'void com.ttt.ddd.sdc_v2.SenosorsDBAdapter.insert(int, java.lang.String, java.lang.String, float, float, float)' on a null object reference
at com.ttt.ddd.sdc_v2.AccelerometerThread.onSensorChanged(AccelerometerThread.java:53)
at android.hardware.SystemSensorManager$SensorEventQueue.dispatchSensorEvent(SystemSensorManager.java:833)
at android.os.MessageQueue.nativePollOnce(Native Method)
at android.os.MessageQueue.next(MessageQueue.java:326)
at android.os.Looper.loop(Looper.java:142)
at android.app.ActivityThread.main(ActivityThread.java:6649)
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:826)
线程
public class AccelerometerThread extends Thread implements Runnable ,SensorEventListener {
private SensorManager mSensorManager;
private Sensor mAccelerometer;
private Context context;
private SenosorsDBAdapter mSensorsDBAdapter;
public AccelerometerThread (Context applicationContext) {
context=applicationContext;
}
@Override
public void run() {
mSensorManager = (SensorManager) context.getSystemService(Context.SENSOR_SERVICE);
if (mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER) != null) {
mAccelerometer = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
mSensorManager.registerListener(this, mAccelerometer, SensorManager.SENSOR_DELAY_GAME);
mSensorsDBAdapter=new SenosorsDBAdapter(context);
}
}
@Override
public void onSensorChanged(SensorEvent event) {
String date;
float x,y,z;
String sensorname=event.sensor.getName();
int sysnsortype=event.sensor.getType();
x=event.values[0];
y=event.values[1];
z=event.values[2];
String sx=Float.toString(x);
String sy=Float.toString(y);
String sz=Float.toString(z);
SimpleDateFormat s = new SimpleDateFormat("dd/MM/yyyy, hh:mm:ss");
date = s.format(new Date());
if(date!=null&&sx!=null&&sy!=null&&sz!=null){
AppConstant.Accelerometer_x=x;
AppConstant.Accelerometer_y=y;
AppConstant.Accelerometer_z=z;
AppConstant.Accelerometer_ST=sysnsortype;
AppConstant.Accelerometer_sensornsme=sensorname;
AppConstant.Accelerometer_date=date;
mSensorsDBAdapter.Accelerometerinsert(sysnsortype,sensorname,日期,X,Y,Z); }
Log.i("Accelerometer_running",date+"=========================>");
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
}