try/catch 无法正常工作并导致应用程序崩溃 android

try/catch not working and crashes app android

我正在向我的 SQLite 数据库中插入一些数据 android。我创建了一个包含内容值的 ArrayList。下面是我的代码.

try {
                SQLiteDatabase db = this.getWritableDatabase();
                db.beginTransaction();
                for (int i = 0; i < arrayListAudio.size(); i++) {
                    db.insert(TABLE_AUDIO_FILES, null, arrayListAudio.get(i));
                }
                for (int i = 0; i < arrayListText.size(); i++) {
                    db.insert(TABLE_TEXT_FILES, null, arrayListText.get(i));
                }
                db.setTransactionSuccessful();
                db.endTransaction();
                db.close();
                //arrayListText.clear();
                //arrayListAudio.clear();
} catch (SQLException e) {
       Log.d("vijaysee", e.getMessage());
}

它可以工作,但有时它不工作并使应用程序崩溃

我的问题是它直接导致应用程序崩溃,因为 try/catch 无法正常工作。如果代码中有任何问题,那么它应该调用 catch,但它会使应用程序崩溃。我认为像 catch(// some change){} 这样的 catch 括号应该有一些变化。请帮我解决这个问题。

您正在捕捉 Java SQLException instead of an Android SQLiteException. And if you don't know what to catch, always try to catch an Exception。我宁愿担心 SQL 语句:INSERT INTO textFiles(null) VALUES (NULL)(既没有列也没有值)。

应用程序崩溃的原因,

  1. 我没有使用同步(在线程中)
  2. 我应该使用 Exception class 而不是指定异常。