我正在尝试让 SQLite 数据库在 Android 上运行。在 MainActivity 中编写了一个小测试,其中包括在数据库一行的 3 个列中输入值。之后,数据被读出并显示到屏幕上以验证它是否有效。
但是,我在 SQLiteOpenHelper.getWritableDatabase() 的 logcat 的堆栈跟踪中得到了一个空指针异常
对我来说没有多大意义,因为创建了数据库。有任何想法吗?
下面显示的是代码的两个部分,扩展 Activity 的 MainActivity 类和具有创建行、读取、更新和删除的所有方法的 Database 类。
Database 类内部是一个名为 SQLiteHelper 的嵌套类,它扩展了 SQLiteOpenHelper。
扩展 Activity 的 MainActivity 类:
public class MainActivity extends Activity {
Button buttonOne;
EditText editTextOne;
EditText editTextTwo;
Context context;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
buttonOne = (Button) findViewById(R.id.button1);
editTextOne = (EditText) findViewById(R.id.edittext1);
editTextTwo = (EditText) findViewById(R.id.edittext2);
//insert new row
Database db = new Database(context);
db.openToWrite();
db.insertNewRow(1, "testlabel", 210);
Toast.makeText(MainActivity.this,"new row inserted", Toast.LENGTH_SHORT).show();
// display all rows
int price = 0;
String name = "";
int status = 0;
String result = "";
db.openToRead();
result = db.getAllFromDB();
db.close();
editTextOne.setText(result);
} // end onCreate
}
嵌套类 SQLiteHelper 扩展了嵌套在数据库类中的 SQLiteOpenHelper
public class SQLiteHelper extends SQLiteOpenHelper {
public SQLiteHelper(Context context, String name,
CursorFactory factory, int version) {
super(context, name, factory, version);
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
db.execSQL(SCRIPT_CREATE_DATABASE);
}
}
}
数据库类:
public class Database {
public static final String MYDATABASE_NAME = "my_database";
public static final String MYDATABASE_TABLE = "my_table";
public static final int MYDATABASE_VERSION = 1;
public static final String KEY_CHECKBOX_STATUS = "check_box_status";
public static final String KEY_CHECKBOX_LABEL = "check_box_label";
public static final String KEY_PRICE = "price";
//create table MY_DATABASE (ID integer primary key, Content text not null);
private static final String SCRIPT_CREATE_DATABASE =
"CREATE TABLE " + MYDATABASE_TABLE + " (" + "ID INTEGER PRIMARY KEY AUTOINCREMENT, " +
"KEY_CHECKBOX_STATUS INTEGER, " + "KEY_CHECKBOX_LABEL TEXT, " + " KEY_PRICE INTEGER" + ");";
SQLiteDatabase sqLiteDatabase;
SQLiteHelper sqLiteHelper;
Context context;
public Database(Context c){
context = c;
}
public void openToRead() throws android.database.SQLException {
sqLiteHelper = new SQLiteHelper(context, MYDATABASE_NAME, null, MYDATABASE_VERSION);
sqLiteDatabase = sqLiteHelper.getReadableDatabase();
}
public void openToWrite() throws android.database.SQLException {
sqLiteHelper = new SQLiteHelper(context, MYDATABASE_NAME, null, MYDATABASE_VERSION);
sqLiteDatabase = sqLiteHelper.getWritableDatabase();
}
public void close(){
sqLiteHelper.close();
}
// after this is all the methods for getting and updating and inserts to database
编辑:来自 Logcat 的完整堆栈跟踪添加如下:
FATAL EXCEPTION:
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.dbtester/com.example.dbtester.MainActivity}: java.lang.NullPointerException
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
android.app.ActivityThread.access$600(ActivityThread.java:123)
android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
android.os.Handler.dispatchMessage(Handler.java:99)
android.os.Looper.loop(Looper.java:137)
android.app.ActivityThread.main(ActivityThread.java:4424)
java.lang.reflect.Method.invokeNative(Native Method)
java.lang.reflect.Method.invoke(Method.java:511)
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
dalvik.system.NativeStart.main(Native Method)
caused by: java.lang.NullPointerException
at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:157)
at com.example.dbtester.Database.openToWrite(Database.java:40)
at com.example.dbtester.MainActivity.onCreate(MainActivity.java:32)
android.app.Activity.performCreate(Activity.java:4465)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)