nullPointerException for SQLiteOpenHelper.getWritableDatabase(), Android

前端 未结 1 1707
我在风中等你
我在风中等你 2021-01-16 10:28

I am trying to get SQLite database to work on Android. Wrote a small test in the MainActivity that consists of entering values in 3 collumns of one row of the database. aft

相关标签:
1条回答
  • 2021-01-16 11:02

    Your context is null change Database db = new Database(context); to Database db = new Database(this); in onCreate

    You should also change your Database constructor so that you do not have to instantiate the SQLiteHelper class every time you open the database.

    public Database(Context c){
          context = c;
          sqLiteHelper = new SQLiteHelper(context, MYDATABASE_NAME, null, MYDATABASE_VERSION);
         }  
    

    And remove sqLiteHelper = new SQLiteHelper(context, MYDATABASE_NAME, null, MYDATABASE_VERSION); from your openToRead() and openToWrite()

    0 讨论(0)
提交回复
热议问题