我正在为我的android应用程序实现一个缓存,并使用sqlite数据库存储服务器响应。处理数据库的类定义如下。它还有一个用作数据库助手的sqliteopenhelper类。
类cachedb是从
public static cachedb cachedb=new cachedb(上下文);
我也在调用方法
cachedb.fetchdatafromdb(“歌曲”);
这些类的代码如下所示。

package com.songs.lookup;

import java.util.ArrayList;
import java.util.List;

import android.annotation.SuppressLint;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteStatement;

public class CacheDB {

    public CacheDB(Context context){
        System.out.println("Before constructing ");
        this.context = context;
        this.dbHelper = new CacheDBHelper(context, DATABASE_NAME, null, DATABASE_VERSION);
        System.out.println("After constructing ");

    }
    private Context context;
    private CacheDBHelper dbHelper;
    private static final String DATABASE_NAME = "";
       private static final int DATABASE_VERSION = 1;
       private static final String song_TABLE_NAME = "songs";
       private static final String tune_TABLE_NAME = "tunes";
       private static final String person_TABLE_NAME = "persons";
       private static final String COLUMN_NAME = "name";

       private static final String song_TABLE_CREATE =
                "CREATE TABLE " + song_TABLE_NAME + " (" +
                COLUMN_NAME + " TEXT);";

       private static final String tune_TABLE_CREATE =
                        "CREATE TABLE " + tune_TABLE_NAME + " (" +
                        COLUMN_NAME + " TEXT);";

       private static final String person_TABLE_CREATE =
                        "CREATE TABLE " + person_TABLE_NAME + " (" +
                        COLUMN_NAME + " TEXT);";
   class CacheDBHelper extends SQLiteOpenHelper{
   public CacheDBHelper(Context context, String name, CursorFactory factory,
            int version) {
       super(context, DATABASE_NAME, null, DATABASE_VERSION);

//     System.out.println("Before the cachedbhelper");
       System.out.println("After the cachedbhelper");

    }

        @Override
        public void onCreate(SQLiteDatabase db) {
            System.out.println("Here inside the oncreate of cacheDBHelper");
            db.execSQL(song_TABLE_CREATE);
            db.execSQL(tune_TABLE_CREATE);
            db.execSQL(person_TABLE_CREATE);

        }

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        }
   }

   @SuppressLint("NewApi")
public void performOperation(String Operation, String table, ArrayList<String> array1)
   {
       SQLiteDatabase db = dbHelper.getWritableDatabase();

       String INSERT = "insert into "
                + table + " (" + COLUMN_NAME + ") values (?)";

       String DELETE = "delete from " + table;

       String FETCH = "select DISTINCT(" + COLUMN_NAME + "from " + table + ")";

       db.beginTransaction();

       SQLiteStatement dbStmt = db.compileStatement(Operation == "INSERT" ? INSERT : DELETE);

       if(Operation == "INSERT")
       {
            int aSize = array1.size();


                for (int i = 0; i < aSize; i++) {
                    dbStmt.bindString(1, array1.get(i));
                    dbStmt.executeInsert();

            }
       }

       if(Operation == "DELETE")
       {
           dbStmt.executeUpdateDelete();

       }

       if(Operation == "SELECT")
       {
           fetchDatafromDB(table);
       }

       db.setTransactionSuccessful();
       db.endTransaction();


       try {
            db.close();
           } catch (Exception e) {
            e.printStackTrace();
           }
        }

   public List<String> fetchDatafromDB(String table) {
        CacheDBHelper dbHelper = new CacheDBHelper(context, DATABASE_NAME, null, DATABASE_VERSION);
        SQLiteDatabase db = dbHelper.getWritableDatabase();

        List<String> list = new ArrayList<String>();
        // Select All Query
        String selectQuery = "SELECT  * FROM " + table;

        Cursor cursor = db.rawQuery(selectQuery, null);

        // looping through all rows and adding to list
        if (cursor.moveToFirst()) {
            do {
                cursor.getString(0);
            } while (cursor.moveToNext());
        }
        // return contact list
        return list;
    }
}

我得到了下面的堆栈跟踪
04-21 00:55:15.188: E/AndroidRuntime(790): Caused by: java.lang.NullPointerException
04-21 00:55:15.188: E/AndroidRuntime(790):  at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:224)
04-21 00:55:15.188: E/AndroidRuntime(790):  at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:164)
04-21 00:55:15.188: E/AndroidRuntime(790):  at com.songs.lookup.CacheDB.fetchDatafromDB(CacheDB.java:124)
04-21 00:55:15.188: E/AndroidRuntime(790):  at com.songs.lookup.LookUpData.getData(LookUpData.java:25)
04-21 00:55:15.188: E/AndroidRuntime(790):  at com.songs.MainActivity2.onCreate(MainActivity2.java:64)
04-21 00:55:15.188: E/AndroidRuntime(790):  at android.app.Activity.performCreate(Activity.java:5008)
04-21 00:55:15.188: E/AndroidRuntime(790):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
04-21 00:55:15.188: E/AndroidRuntime(790):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)

这里的问题在哪里?

最佳答案

我之前也遇到过类似的问题,我的问题是因为我试图在一个之前已经打开过的方法中打开一个数据库。我不知道这是否有意义,但我可以在这里看到您在第一次调用dbHelper.getWritableDatabase()方法后两次都没有关闭数据库。
我认为这可能是问题的原因,因为数据库被前一个方法锁定,因此出现了错误

at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:224)

不太确定我是不是100%正确,但希望这有帮助。干杯

07-27 17:06