0

为什么我没有收到 SQLite 失败的错误代码?我正在尝试创建一个数据库并获取以下堆栈跟踪(嗯,跟踪的顶部):

E/SqliteDatabaseCpp(11974): sqlite3_open_v2
("/mnt/sdcard/myapp/mydatabase.db", &handle, 6, NULL) failed
E/SQLiteDatabase(11974): Failed to open the database. closing it.
E/SQLiteDatabase(11974): android.database.sqlite.SQLiteCantOpenDatabaseException: unable to open database file
E/SQLiteDatabase(11974):    at android.database.sqlite.SQLiteDatabase.dbopen(Native Method)

我的代码在模拟器中工作,在 Nexus 7 上工作,但在另一台设备上失败

  public SQLiteDatabase openOrCreateDatabase (String name, int mode,
                                              SQLiteDatabase.CursorFactory factory,
                                              DatabaseErrorHandler errorHandler)
  {
    File f = getDatabasePath (name);
    System.out.println ("opening db: " + f.getPath());
    return super.openOrCreateDatabase (f.getPath(), mode, factory, errorHandler);
  }

这是第二次尝试的完整日志。它与第一个有点不同,但仍然没有错误代码。

W/dalvikvm( 3314): threadid=1: thread exiting with uncaught exception (group=0x40a801f8)
E/AndroidRuntime( 3314): FATAL EXCEPTION: main
E/AndroidRuntime( 3314): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.myapp.myapp/com.myapp.myapp.main.MainActivity}: android.database.sqlite.SQLiteCantOpenDatabaseException: unable to open database file
E/AndroidRuntime( 3314):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
E/AndroidRuntime( 3314):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
E/AndroidRuntime( 3314):    at android.app.ActivityThread.access$600(ActivityThread.java:123)
E/AndroidRuntime( 3314):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
E/AndroidRuntime( 3314):    at android.os.Handler.dispatchMessage(Handler.java:99)
E/AndroidRuntime( 3314):    at android.os.Looper.loop(Looper.java:137)
E/AndroidRuntime( 3314):    at android.app.ActivityThread.main(ActivityThread.java:4424)
E/AndroidRuntime( 3314):    at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime( 3314):    at java.lang.reflect.Method.invoke(Method.java:511)
E/AndroidRuntime( 3314):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:812)
E/AndroidRuntime( 3314):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:579)
E/AndroidRuntime( 3314):    at dalvik.system.NativeStart.main(Native Method)
E/AndroidRuntime( 3314): Caused by: android.database.sqlite.SQLiteCantOpenDatabaseException: unable to open database file
E/AndroidRuntime( 3314):    at android.database.sqlite.SQLiteDatabase.dbopen(Native Method)
E/AndroidRuntime( 3314):    at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:1013)
E/AndroidRuntime( 3314):    at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:986)
E/AndroidRuntime( 3314):    at android.database.sqlite.SQLiteDatabase.openOrCreateDatabase(SQLiteDatabase.java:1051)
E/AndroidRuntime( 3314):    at android.app.ContextImpl.openOrCreateDatabase(ContextImpl.java:782)
E/AndroidRuntime( 3314):    at android.content.ContextWrapper.openOrCreateDatabase(ContextWrapper.java:221)
E/AndroidRuntime( 3314):    at android.content.ContextWrapper.openOrCreateDatabase(ContextWrapper.java:221)
E/AndroidRuntime( 3314):    at com.myapp.myapp.dbaccess.DatabaseContext.openOrCreateDatabase(DbHelper.java:63)
E/AndroidRuntime( 3314):    at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:157)
E/AndroidRuntime( 3314):    at com.myapp.myapp.dbaccess.DbHelper.getDb(DbHelper.java:184)
E/AndroidRuntime( 3314):    at com.myapp.myapp.dbaccess.DbHelper.getZRoot(DbHelper.java:650)
E/AndroidRuntime( 3314):    at com.myapp.myapp.dbaccess.ZNodeCache.getRequiredNodes(ZNodeCache.java:37)
E/AndroidRuntime( 3314):    at com.myapp.myapp.dbaccess.ZNodeCache.<init>(ZNodeCache.java:22)
E/AndroidRuntime( 3314):    at com.myapp.myapp.main.MainActivity.onCreate(MainActivity.java:81)
E/AndroidRuntime( 3314):    at android.app.Activity.performCreate(Activity.java:4465)
E/AndroidRuntime( 3314):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
E/AndroidRuntime( 3314):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
E/AndroidRuntime( 3314):    ... 11 more
4

1 回答 1

0

好吧,这很愚蠢。如果您已连接设备并选择“USB 存储”连接,则 SD 卡将无法访问。故事的寓意:如果您的应用程序访问 sdcard,请不要启用“USB 存储”。

尽管如此,Android 应该会发出一个错误代码——我会提交一个错误。

于 2012-12-05T01:25:58.310 回答