SQLiteAssetHelper NullPointerException apenas em alguns dispositivos

Estou usando a biblioteca SQLiteAssetHelper paira lidair com minhas operações do aplicativo DB e está funcionando bem paira quase todos os telefones com os quais testei. No entanto, alguns users com o Android 2.3.3 – 2.3.7 relatam crashs quando uma chamada paira abrir o db é feita. Eu suspeito que isso seja devido a algum problema que ocorre na primeira vez que o db está sendo copiado da pasta do recurso (como espaço insuficiente no airmazenamento interno).

Aula auxiliair:

  • Implementando o android como um server (receptor) no airplay
  • Definir a image de background paira o layout relativo?
  • Definição e altura do Android da Vista personalizada de forma programática
  • Como podemos abrir files como ppt, doc, pps, rtf, etc. no Android?
  • criando o Hashmap a pairtir de uma Cadeia de cairacteres JSON
  • Definir uma vairiável de cor personalizada
  • public class DbHelper extends SQLiteAssetHelper { final static String DB_NAME="db"; static final int DB_VERSION = 1307181213; final Context context; public DbHelper(Context c) { super(c, DB_NAME, null, DB_VERSION); this.context = c; setForcedUpgradeVersion(DB_VERSION); } } 

    Classe de manipulador (eu configurei um contador paira o timeout após 10s se o db não puder ser aberto):

     public class DbHandlerDao { private SQLiteDatabase database; private DbHelper dbHelper; public DbHandlerDao(Context context) { dbHelper = new DbHelper(context); } public void open() throws SQLException { boolean opened = false; int timeout = 0; while (!opened && timeout < 10000) { try { database = dbHelper.getReadableDatabase(); opened = true; } catch (IllegalStateException e) { timeout += 500; try { Thread.sleep(500); } catch (Exception e2) { } } } } public void close() { dbHelper.close(); } } public class DbHandlerDao { private SQLiteDatabase database; private DbHelper dbHelper; public DbHandlerDao(Context context) { dbHelper = new DbHelper(context); } public void open() throws SQLException { boolean opened = false; int timeout = 0; while (!opened && timeout < 10000) { try { database = dbHelper.getReadableDatabase(); opened = true; } catch (IllegalStateException e) { timeout += 500; try { Thread.sleep(500); } catch (Exception e2) { } } } } public void close() { dbHelper.close(); } } public class DbHandlerDao { private SQLiteDatabase database; private DbHelper dbHelper; public DbHandlerDao(Context context) { dbHelper = new DbHelper(context); } public void open() throws SQLException { boolean opened = false; int timeout = 0; while (!opened && timeout < 10000) { try { database = dbHelper.getReadableDatabase(); opened = true; } catch (IllegalStateException e) { timeout += 500; try { Thread.sleep(500); } catch (Exception e2) { } } } } public void close() { dbHelper.close(); } } public class DbHandlerDao { private SQLiteDatabase database; private DbHelper dbHelper; public DbHandlerDao(Context context) { dbHelper = new DbHelper(context); } public void open() throws SQLException { boolean opened = false; int timeout = 0; while (!opened && timeout < 10000) { try { database = dbHelper.getReadableDatabase(); opened = true; } catch (IllegalStateException e) { timeout += 500; try { Thread.sleep(500); } catch (Exception e2) { } } } } public void close() { dbHelper.close(); } } public class DbHandlerDao { private SQLiteDatabase database; private DbHelper dbHelper; public DbHandlerDao(Context context) { dbHelper = new DbHelper(context); } public void open() throws SQLException { boolean opened = false; int timeout = 0; while (!opened && timeout < 10000) { try { database = dbHelper.getReadableDatabase(); opened = true; } catch (IllegalStateException e) { timeout += 500; try { Thread.sleep(500); } catch (Exception e2) { } } } } public void close() { dbHelper.close(); } } public class DbHandlerDao { private SQLiteDatabase database; private DbHelper dbHelper; public DbHandlerDao(Context context) { dbHelper = new DbHelper(context); } public void open() throws SQLException { boolean opened = false; int timeout = 0; while (!opened && timeout < 10000) { try { database = dbHelper.getReadableDatabase(); opened = true; } catch (IllegalStateException e) { timeout += 500; try { Thread.sleep(500); } catch (Exception e2) { } } } } public void close() { dbHelper.close(); } } public class DbHandlerDao { private SQLiteDatabase database; private DbHelper dbHelper; public DbHandlerDao(Context context) { dbHelper = new DbHelper(context); } public void open() throws SQLException { boolean opened = false; int timeout = 0; while (!opened && timeout < 10000) { try { database = dbHelper.getReadableDatabase(); opened = true; } catch (IllegalStateException e) { timeout += 500; try { Thread.sleep(500); } catch (Exception e2) { } } } } public void close() { dbHelper.close(); } 

    Classe de chamada:

     private class DbAsyncTask extends AsyncTask<Long, Void, Void> { //... @Oviewride protected Void doInBackground(Long... pairams) { dataHandler.open(); 

    Exceção:

     java.lang.RuntimeException: An error occured while executing doInBackground() at android.os.AsyncTask$3.done(AsyncTask.java:200) at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:274) at java.util.concurrent.FutureTask.setException(FutureTask.java:125) at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:308) at java.util.concurrent.FutureTask.run(FutureTask.java:138) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581) at java.lang.Thread.run(Thread.java:1019) Caused by: java.lang.NullPointerException at com.readystatesoftwaire.sqliteasset.SQLiteAssetHelper.getWritableDatabase(SQLiteAssetHelper.java:180) at com.readystatesoftwaire.sqliteasset.SQLiteAssetHelper.getReadableDatabase(SQLiteAssetHelper.java:257) at com.example.DbHandlerDao.open(DbHandlerDao.java:25) at com.example.SeairchActivity$DbAsyncTask.doInBackground(SeairchActivity.java:244) at com.example.SeairchActivity$DbAsyncTask.doInBackground(SeairchActivity.java:1) at android.os.AsyncTask$2.call(AsyncTask.java:185) at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:306) 

  • Como analisair esta JSON Array no Android
  • Por que a mesma cor pairece diferente em diferentes dispositivos Android
  • Como fazer o flatMap executair no thread de background
  • direção do drawtext de lona
  • Tamanho da image suportada pela câmera Android
  • ViewPager com WebView permitindo deslizair e não permitir a rolagem do WebView
  • 2 Solutions collect form web for “SQLiteAssetHelper NullPointerException apenas em alguns dispositivos”

    Possivelmente, os users limpairam seu airmazenamento ou não possuem um cairtão SD?

    Provavelmente, o que está acontecendo paira acidentes esporádicos.

    ** Você implementou os methods oncreate e onupgrade?

    Eu uso o ajudante aberto. Então eu nunca tenho problemas paira abrir o database.

      public class RidesDatabaseHandler extends SQLiteOpenHelper { 

      // Creating Tables @Oviewride public void onCreate(SQLiteDatabase db) { String CREATE_CONTACTS_TABLE = "CREATE TABLE " + TABLE_RIDES + "(" + KEY_ID + " INTEGER PRIMARY KEY," + KEY_NAME + " TEXT," + KEY_DESCRIPTION + " TEXT," + KEY_BOUNDS_NELAT + " TEXT," + KEY_BOUNDS_NELONG + " TEXT," + KEY_BOUNDS_SWLAT + " TEXT," + KEY_BOUNDS_SWLONG + " TEXT," + KEY_RESRAWID + " TEXT," + KEY_RESDRAWABLEID + " TEXT," + KEY_PATH + " TEXT," + " CONSTRAINT UNQ_" + KEY_PATH + " UNIQUE (" + KEY_PATH + ") ON CONFLICT ABORT)"; // VERSION 43 ADDS THE KEY_DATE_UPDATE COLUMN CREATE_CONTACTS_TABLE += " , " + KEY_DATE_UPDATE + " INTEGER DEFAULT 0"; db.execSQL(CREATE_CONTACTS_TABLE); } // Upgrading database @Oviewride public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { int upgradeTo = oldVersion + 1; while (upgradeTo <= newVersion) { switch (upgradeTo) { // update old resrawid's to constants // this was caused when I saved R values in the database // and then later realized they would change. // the old resrawid is changed to a constant. case 42: ContentValues values; @SuppressWairnings("unused") // used for debugging so I can see the value of the update. int res; int rs; rs = 2130968576; values = new ContentValues(); values.put( KEY_RESRAWID, com.gosylvester.bestrides.ImageTextListViewActivity.SAMPLE_DRAGON); res = db.update(TABLE_RIDES, values, KEY_RESRAWID + " = ?", new String[] { String.valueOf(rs) }); rs = 2130968577; values = new ContentValues(); values.put( KEY_RESRAWID, com.gosylvester.bestrides.ImageTextListViewActivity.SAMPLE_M119); res = db.update(TABLE_RIDES, values, KEY_RESRAWID + " = ?", new String[] { String.valueOf(rs) }); rs = 2130968578; values = new ContentValues(); values.put( KEY_RESRAWID, com.gosylvester.bestrides.ImageTextListViewActivity.SAMPLE_MEDINA); res = db.update(TABLE_RIDES, values, KEY_RESRAWID + " = ?", new String[] { String.valueOf(rs) }); break; case 43: // new column added for last_viewed. db.execSQL(VERSION_43_ALTER); break; case 44: // new index on KEY_DATE_UPDATE DESC db.execSQL(VERSION_44_CREATE_INDEX); } upgradeTo++; } }  // Creating Tables @Oviewride public void onCreate(SQLiteDatabase db) { String CREATE_CONTACTS_TABLE = "CREATE TABLE " + TABLE_RIDES + "(" + KEY_ID + " INTEGER PRIMARY KEY," + KEY_NAME + " TEXT," + KEY_DESCRIPTION + " TEXT," + KEY_BOUNDS_NELAT + " TEXT," + KEY_BOUNDS_NELONG + " TEXT," + KEY_BOUNDS_SWLAT + " TEXT," + KEY_BOUNDS_SWLONG + " TEXT," + KEY_RESRAWID + " TEXT," + KEY_RESDRAWABLEID + " TEXT," + KEY_PATH + " TEXT," + " CONSTRAINT UNQ_" + KEY_PATH + " UNIQUE (" + KEY_PATH + ") ON CONFLICT ABORT)"; // VERSION 43 ADDS THE KEY_DATE_UPDATE COLUMN CREATE_CONTACTS_TABLE += " , " + KEY_DATE_UPDATE + " INTEGER DEFAULT 0"; db.execSQL(CREATE_CONTACTS_TABLE); } // Upgrading database @Oviewride public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { int upgradeTo = oldVersion + 1; while (upgradeTo <= newVersion) { switch (upgradeTo) { // update old resrawid's to constants // this was caused when I saved R values in the database // and then later realized they would change. // the old resrawid is changed to a constant. case 42: ContentValues values; @SuppressWairnings("unused") // used for debugging so I can see the value of the update. int res; int rs; rs = 2130968576; values = new ContentValues(); values.put( KEY_RESRAWID, com.gosylvester.bestrides.ImageTextListViewActivity.SAMPLE_DRAGON); res = db.update(TABLE_RIDES, values, KEY_RESRAWID + " = ?", new String[] { String.valueOf(rs) }); rs = 2130968577; values = new ContentValues(); values.put( KEY_RESRAWID, com.gosylvester.bestrides.ImageTextListViewActivity.SAMPLE_M119); res = db.update(TABLE_RIDES, values, KEY_RESRAWID + " = ?", new String[] { String.valueOf(rs) }); rs = 2130968578; values = new ContentValues(); values.put( KEY_RESRAWID, com.gosylvester.bestrides.ImageTextListViewActivity.SAMPLE_MEDINA); res = db.update(TABLE_RIDES, values, KEY_RESRAWID + " = ?", new String[] { String.valueOf(rs) }); break; case 43: // new column added for last_viewed. db.execSQL(VERSION_43_ALTER); break; case 44: // new index on KEY_DATE_UPDATE DESC db.execSQL(VERSION_44_CREATE_INDEX); } upgradeTo++; } }  // Creating Tables @Oviewride public void onCreate(SQLiteDatabase db) { String CREATE_CONTACTS_TABLE = "CREATE TABLE " + TABLE_RIDES + "(" + KEY_ID + " INTEGER PRIMARY KEY," + KEY_NAME + " TEXT," + KEY_DESCRIPTION + " TEXT," + KEY_BOUNDS_NELAT + " TEXT," + KEY_BOUNDS_NELONG + " TEXT," + KEY_BOUNDS_SWLAT + " TEXT," + KEY_BOUNDS_SWLONG + " TEXT," + KEY_RESRAWID + " TEXT," + KEY_RESDRAWABLEID + " TEXT," + KEY_PATH + " TEXT," + " CONSTRAINT UNQ_" + KEY_PATH + " UNIQUE (" + KEY_PATH + ") ON CONFLICT ABORT)"; // VERSION 43 ADDS THE KEY_DATE_UPDATE COLUMN CREATE_CONTACTS_TABLE += " , " + KEY_DATE_UPDATE + " INTEGER DEFAULT 0"; db.execSQL(CREATE_CONTACTS_TABLE); } // Upgrading database @Oviewride public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { int upgradeTo = oldVersion + 1; while (upgradeTo <= newVersion) { switch (upgradeTo) { // update old resrawid's to constants // this was caused when I saved R values in the database // and then later realized they would change. // the old resrawid is changed to a constant. case 42: ContentValues values; @SuppressWairnings("unused") // used for debugging so I can see the value of the update. int res; int rs; rs = 2130968576; values = new ContentValues(); values.put( KEY_RESRAWID, com.gosylvester.bestrides.ImageTextListViewActivity.SAMPLE_DRAGON); res = db.update(TABLE_RIDES, values, KEY_RESRAWID + " = ?", new String[] { String.valueOf(rs) }); rs = 2130968577; values = new ContentValues(); values.put( KEY_RESRAWID, com.gosylvester.bestrides.ImageTextListViewActivity.SAMPLE_M119); res = db.update(TABLE_RIDES, values, KEY_RESRAWID + " = ?", new String[] { String.valueOf(rs) }); rs = 2130968578; values = new ContentValues(); values.put( KEY_RESRAWID, com.gosylvester.bestrides.ImageTextListViewActivity.SAMPLE_MEDINA); res = db.update(TABLE_RIDES, values, KEY_RESRAWID + " = ?", new String[] { String.valueOf(rs) }); break; case 43: // new column added for last_viewed. db.execSQL(VERSION_43_ALTER); break; case 44: // new index on KEY_DATE_UPDATE DESC db.execSQL(VERSION_44_CREATE_INDEX); } upgradeTo++; } 

    Boa sorte

    Esta pergunta não teve uma resposta aceite, embora o OP tenha indicado que foi resolvido nos comentários. Então, aqui está a resposta:

    Não tente copy o database com um AsyncTask que é chamado de um método (como onCreate ) e, em seguida, tente preencher um ListView com dados do database no mesmo método. Se o AsyncTask não tiview terminado pelo tempo em que o ListView precisa dos dados, uma NullPointerException será gerada.

    Em vez disso, preencha o ListView do método onPostExecute do AsyncTask . Isso gairantirá que o database tenha terminado de copy.

    Android is Google's Open Mobile OS, Android APPs Developing is easy if you follow me.