GetReadableDatabase错误android

时间:2011-01-13 00:56:12

标签: android database sqlite

我创建了一个DBTest类SQLiteOpenHelper

然后我从我的主UI中调用了它。

当我点击DB.GetReadableDatabase()并且日志没有帮助时崩溃,只是说空指针,但我不知道在哪里看。

如果我使用

,一切正常
SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);

相同数据库

以下是帮助程序的类和错误:

   public class DBTest extends SQLiteOpenHelper {

            private static String DB_NAME = "DB";

            private SQLiteDatabase myDataBase; 

            private final Context myContext;

            public DBTest(Context context) {

                super(context,   DB_NAME, null, 1);
                this.myContext = context;
            }

            @Override
            public void onCreate(SQLiteDatabase db) {
                // TODO Auto-generated method stub

            }

            @Override
            public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
                // TODO Auto-generated method stub

            }
    }

以下是错误发生的地方:

public class Main extends Activity 
{

    DataBaseHelper db = new DataBaseHelper(null);
    static SQLiteDatabase Db;



    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        SQLiteDatabase Db;

        DBTest db1 = new DBTest(null);

        Db = db1.getReadableDatabase(); <<<  blow up here


        }
}

1 个答案:

答案 0 :(得分:4)

DBTest db1 = new DBTest(this.getApplicationContext());

上下文不能为空