这里我正在制作数据库光标的功能,这在项目中经常使用。
这是我在DAL中的代码:
public Cursor SelectQuery(String table){
Cursor cursor=SQLiteDb.query(table,null,null,null,null,null,null,null);
return cursor;
}
通过在mainactivity中调试 SQLiteDB变为空。
代码:
Cursor c=dal.SelectQuery(ConstantsValues.TabData);
Log.e("hello111",""+c.getCount());
以便获得总行数但是表格没有到来。这里 ConstantsValues.TabData有表格名称。
在这里的代码:
public ArrayList AllSelectQryForTabEmpData1() {
ArrayList<Employee> data = new ArrayList();
Cursor cursor = SQLiteDb.query(ConstantsValues.TabData, null, null, null, null, null, null, null);
while (cursor.moveToNext()) {
String id = cursor.getString(cursor.getColumnIndex(ConstantsValues.ID));
String name = cursor.getString(cursor.getColumnIndex(ConstantsValues.NAME));
String value= cursor.getString(cursor.getColumnIndex(ConstantsValues.VALUE));
data.add(new Employee(id, name,value));
}
cursor.close();
return data;
}
这是打开和关闭的代码:
public void OpenDB() {
SQLiteDb = new DBHelper(context).getWritableDatabase();
}
public void CloseDB() {
if (SQLiteDb.isOpen()) {
SQLiteDb.close();
}
}
我在SQLiteDb中获取Table。制作光标似乎有些不对劲。
这是我的 logcat :
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.abhishek.httpclient, PID: 12538
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.abhishek.httpclient/com.example.abhishek.httpclient.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.database.Cursor android.database.sqlite.SQLiteDatabase.query(java.lang.String, java.lang.String[], java.lang.String, java.lang.String[], java.lang.String, java.lang.String, java.lang.String, java.lang.String)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2585)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2671)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1501)
at android.os.Handler.dispatchMessage(Handler.java:111)
at android.os.Looper.loop(Looper.java:207)
at android.app.ActivityThread.main(ActivityThread.java:5769)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:681)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.database.Cursor android.database.sqlite.SQLiteDatabase.query(java.lang.String, java.lang.String[], java.lang.String, java.lang.String[], java.lang.String, java.lang.String, java.lang.String, java.lang.String)' on a null object reference
at com.example.abhishek.httpclient.DAL.SelectQuery(DAL.java:141)
at com.example.abhishek.httpclient.MainActivity.onCreate(MainActivity.java:92)
at android.app.Activity.performCreate(Activity.java:6317)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1136)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2532)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2671)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1501)
at android.os.Handler.dispatchMessage(Handler.java:111)
at android.os.Looper.loop(Looper.java:207)
at android.app.ActivityThread.main(ActivityThread.java:5769)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:681)
答案 0 :(得分:3)
首先需要实例化 SQLiteDatabase ,如下所示:
// Create and/or open the database for writing
SQLiteDatabase db = getWritableDatabase();
<强>更新强>
要进行简单的修复,您可以将openDB()
方法更改为(注意:未测试):
public void openDB() {
if(sqliteDB == null) {
sqliteDB= new DBHelper(context).getWritableDatabase();
} else {
if(!sqliteDB.isOpen(){
// reopen again.
// NOTE: This should be changed to the method which you use to open the db.
sqliteDB= new DBHelper(context).getWritableDatabase();
}
}
}
每次使用SQLiteDb时都会调用它,如下所示:
openDB();
Cursor cursor = SQLiteDb.query(ConstantsValues.TabData, null, null,
null, null, null, null, null);
...
答案 1 :(得分:2)
我认为数据库连接有问题。请特别关注本教程DBHelper.java类。他们如何建立数据库连接。 https://www.tutorialspoint.com/android/android_sqlite_database.htm
SQLiteDatabase db = this.getWritableDatabase();
通过使用getWritableDatabase(),您可以对数据库执行读写操作。