SQLiteCantOpenDatabaseException:发生在一个用户帐户上,但不发生在另一个用户帐户上

时间:2015-04-16 13:51:50

标签: android sqlite sqliteopenhelper galaxy-tab

这是我的应用中的边缘情况崩溃。用户有一个运行Android 4.4的Galaxy Tab4。该应用程序可以在其设备的某个用户帐户上正常运行,但在另一个帐户中,它在尝试创建数据库时首次运行时始终崩溃。我使用SQLiteOpenHelper调用getReadableDatabase,它与SQLiteCantOpenDatabaseException崩溃。

public class DBAccess extends SQLiteOpenHelper {
    private static DBAccess mInstance = null;
    private static String DB_NAME = "scripturetyper.db";
    private final Context myContext;
    private String sDefaultUserName = "";

    public static DBAccess getInstance(Context context){
        if(mInstance == null){
            mInstance = new DBAccess(context.getApplicationContext());
        }
        return mInstance;
    }

    public DBAccess(Context context) {
        super(context, DB_NAME, null, 1);
        this.myContext = context;
        this.close();
        try {
            this.createDataBase();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    // Creates a empty database on the system and rewrites it with your own database.
    public void createDataBase() throws IOException{
        boolean dbExist = checkDataBase();
        if(dbExist){
            //do nothing - database already exist
        }else{
            //By calling this method an empty database will be created into the default system path
               //of your application so we are gonna be able to overwrite that database with our database.
            this.getReadableDatabase(); //line 58
            this.close();
            copyDataBase();
        }
    }

    // Check if the database already exist to avoid re-copying the file each time you open the application.
    // @return true if it exists, false if it does not
    private boolean checkDataBase(){
        SQLiteDatabase checkDB = null;
        try{
            checkDB = SQLiteDatabase.openDatabase(myContext.getDatabasePath(DB_NAME).getAbsolutePath(), null, SQLiteDatabase.NO_LOCALIZED_COLLATORS);
        }catch(SQLiteException e){
            //database does't exist yet.
        }

        if(checkDB != null){
            checkDB.close();
        }
        return checkDB != null ? true : false;
    }
    ...

堆栈追踪:

java.lang.RuntimeException:无法启动活动ComponentInfo {com.millennialsolutions.scripturetyper / com.millennialsolutions.scripturetyper.ActivityMain}: android.database.sqlite.SQLiteCantOpenDatabaseException:未知错误(代码14):无法打开数据库     在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2331)     在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2389)     在android.app.ActivityThread.access $ 900(ActivityThread.java:169)     在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1277)     在android.os.Handler.dispatchMessage(Handler.java:102)     在android.os.Looper.loop(Looper.java:136)     在android.app.ActivityThread.main(ActivityThread.java:5479)     at java.lang.reflect.Method.invokeNative(Native Method)     在java.lang.reflect.Method.invoke(Method.java:515)     在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:1283)     在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)     at dalvik.system.NativeStart.main(Native Method)   引起:android.database.sqlite.SQLiteCantOpenDatabaseException:未知错误(代码14):无法打开数据库     在android.database.sqlite.SQLiteConnection.nativeOpen(本机方法)     在android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:338)     在android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:232)     在android.database.sqlite.SQLiteConnectionPool.openConnectionLocked(SQLiteConnectionPool.java:512)     在android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:206)     在android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:178)     在android.database.sqlite.SQLiteDatabase.openInner(SQLiteDatabase.java:902)     在android.database.sqlite.SQLiteDatabase.open(SQLiteDatabase.java:872)     在android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:709)     在android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:235)     在android.database.sqlite.SQLiteOpenHelper.getReadableDatabase(SQLiteOpenHelper.java:188)     在com.millennialsolutions.scripturetyper.DBAccess.createDataBase(DBAccess.java:58)     在com.millennialsolutions.scripturetyper.DBAccess。(DBAccess.java:43)     在com.millennialsolutions.scripturetyper.DBAccess.getInstance(DBAccess.java:32)     at com.millennialsolutions.scripturetyper.Query.ExecuteString(Query.java:206)     在com.millennialsolutions.scripturetyper.DBUpdates.RunDbUpdates(DBUpdates.java:20)     在com.millennialsolutions.scripturetyper.ActivityMain.LoadApp(ActivityMain.java:854)     在com.millennialsolutions.scripturetyper.ActivityMain.onCreate(ActivityMain.java:646)     在android.app.Activity.performCreate(Activity.java:5451)     在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1093)     在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2295)

0 个答案:

没有答案