我创建了一个应用程序,将某些数据存储在数据库中(SQLite),但我遇到的问题是,在存储数据时,我得到NullPointerException
,(Logcat附加)。我面临的问题是SQLiteDatabse db=this.getWritableDatabase();
我已经搜索了几乎所有的解决方案,但没有人帮忙,如果有人能提供帮助,那将是一个很大的帮助。
由于
package com.example.neuronsdk;
import android.content.ContentValues;
import android.content.Context;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
public class DatabaseHandler extends SQLiteOpenHelper {
private static final int DATABASE_VERSION = 2;
private static final String DATABASE_NAME = "NeuronDatabase";
private static final String TABLE_EVENTS = "Events";
private static final String TAG = "DatabaseHandler.class";
private static final String KEY_ID = "id";
private static final String KEY_EVENTS = "eventsText";
private static final String KEY_START_TIME="start_time";
private static final String KEY_END_TIME="end_time";
String url="www.neuronme.com/android_sdk_data/";
SQLiteDatabase dbHelper=null;
DatabaseHandler dbh;
private Context context;
private static final String NEURON_DATABASE ="CREATE TABLE " + TABLE_EVENTS + "(" +
KEY_EVENTS + " TEXT NOT NULL, " + KEY_START_TIME + " INTEGER," +
KEY_END_TIME + " INTEGER );";
public DatabaseHandler(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
this.context=context;
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
db.execSQL(NEURON_DATABASE);
Log.d(TAG, "Database Created");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
db.execSQL("DROP TABLE IF EXISTS " + TABLE_EVENTS);
this.onCreate(db);
}
public void open() throws SQLException {
// close();
//dbh=new DatabaseHandler(this.context);
dbHelper = this.getWritableDatabase();
}
public void addEvent(String eventText){
ContentValues values = new ContentValues();
Log.d("Database", "Adding value in database"+ eventText);
open();
if(eventText != null)
{
values.put(KEY_EVENTS, eventText);
dbHelper.insert(TABLE_EVENTS, null, values);
close();
}
}
public void addStartTime(Long startTime){
ContentValues values = new ContentValues();
Log.d("Database", "Adding start time in database"+ startTime);
close();
//dbHelper=this.getWritableDatabase();
values.put(KEY_START_TIME, startTime);
dbHelper.insert(TABLE_EVENTS, null, values);
close();
}
public void addEndTime(Long endTime){
ContentValues values = new ContentValues();
Log.d("Database", "Adding End time in database"+ endTime);
close();
// dbHelper=getWritableDatabase();
values.put(KEY_END_TIME, endTime);
dbHelper.insert(TABLE_EVENTS, null, values);
close();
}
}
Logcat的错误报告是:
07-16 07:08:32.435: E/AndroidRuntime(1869): FATAL EXCEPTION: main
07-16 07:08:32.435: E/AndroidRuntime(1869): Process: com.example.test_application, PID: 1869
07-16 07:08:32.435: E/AndroidRuntime(1869): java.lang.NullPointerException
07-16 07:08:32.435: E/AndroidRuntime(1869): at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:224)
07-16 07:08:32.435: E/AndroidRuntime(1869): at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:164)
07-16 07:08:32.435: E/AndroidRuntime(1869): at com.example.neuronsdk.DatabaseHandler.open(DatabaseHandler.java:59)
07-16 07:08:32.435: E/AndroidRuntime(1869): at com.example.neuronsdk.DatabaseHandler.addEvent(DatabaseHandler.java:72)
07-16 07:08:32.435: E/AndroidRuntime(1869): at com.example.neuronsdk.NeuronActivity.onClick(NeuronActivity.java:28)
07-16 07:08:32.435: E/AndroidRuntime(1869): at com.example.test_application.MainActivity$1.onClick(MainActivity.java:47)
07-16 07:08:32.435: E/AndroidRuntime(1869): at android.view.View.performClick(View.java:4438)
07-16 07:08:32.435: E/AndroidRuntime(1869): at android.view.View$PerformClick.run(View.java:18422)
07-16 07:08:32.435: E/AndroidRuntime(1869): at android.os.Handler.handleCallback(Handler.java:733)
07-16 07:08:32.435: E/AndroidRuntime(1869): at android.os.Handler.dispatchMessage(Handler.java:95)
07-16 07:08:32.435: E/AndroidRuntime(1869): at android.os.Looper.loop(Looper.java:136)
07-16 07:08:32.435: E/AndroidRuntime(1869): at android.app.ActivityThread.main(ActivityThread.java:5017)
07-16 07:08:32.435: E/AndroidRuntime(1869): at java.lang.reflect.Method.invokeNative(Native Method)
07-16 07:08:32.435: E/AndroidRuntime(1869): at java.lang.reflect.Method.invoke(Method.java:515)
07-16 07:08:32.435: E/AndroidRuntime(1869): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
07-16 07:08:32.435: E/AndroidRuntime(1869): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
07-16 07:08:32.435: E/AndroidRuntime(1869): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:1)
您正在null
传递Context
:
Context context = null;
//this.context=context;
dbh=new DatabaseHandler(context);
您需要传递有效Context
,例如Application
或Activity
。
答案 1 :(得分:1)
首先在构造函数中指定上下文值
public DatabaseHandler(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
this.context=context;
}
然后在你的open方法中替换
dbh=new DatabaseHandler(context);
与
dbh=new DatabaseHandler(this.context);
答案 2 :(得分:1)
将构造函数更改为:
template <typename T>
void func_1(std::initializer_list<T>&& a) {
// do something
}
template <typename... T>
void func(T&&... a) {
func_1({std::forward<T>(a)...});
}
int main() {
func(1, 2, 3);
// func(1, 2, 3, 4.0); // OK doesn't compile
}
将open()方法更改为:
public DatabaseHandler(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
this.context = context;
}
实施closeDB()方法
public void open() throws SQLException {
close();
this.getWritableDatabase();
}
使用应用程序上下文并使用已存在的实例。也就是说,不是调用构造函数,而是调用实例。见下文:
public void closeDB() {
SQLiteDatabase db = this.getReadableDatabase();
if (db != null && db.isOpen())
db.close();
}