应用程序的工作方式就像模拟器上的魅力,而android在设备中强制关闭

时间:2012-05-04 06:47:05

标签: android sqlite

这是我的dbAdapter类

public class DBAdapter extends SQLiteOpenHelper {

private static String DB_PATH = "";
private static final String DB_NAME = "v2v.sqlite";
private SQLiteDatabase myDataBase;
private final Context myContext;

private static DBAdapter mDBConnection;

DBAdapter(Context context) {
    super(context, DB_NAME, null, 1);
    this.myContext = context;
    DB_PATH = "/data/data/com.lauruss.verb2verbe/databases/";
}

public static synchronized DBAdapter getDBAdapterInstance(Context context) {
    if (mDBConnection == null) {
        mDBConnection = new DBAdapter(context);
    }
    return mDBConnection;
}

public void createDataBase() throws IOException {

    boolean dbExist = checkDataBase();

    if (dbExist) {
        // do nothing - database already exist
    } else {

        this.getReadableDatabase();
        try {
            copyDataBase();
        } catch (IOException e) {
            throw new Error("Error copying database");
        }
    }
}

private boolean checkDataBase() {
    /*SQLiteDatabase checkDB = null;
    try {
        String myPath = DB_PATH + DB_NAME;
        checkDB = SQLiteDatabase.openDatabase(myPath, null,
                SQLiteDatabase.OPEN_READONLY);

    } catch (SQLiteException e) {
        // database does't exist yet.
    }
    if (checkDB != null) {
        checkDB.close();
    }
    return checkDB != null ? true : false;*/
/// New changed code
    File dbFile = new File(DB_PATH + DB_NAME);
    return dbFile.exists();
}

private void copyDataBase() throws IOException {
    InputStream myInput;
    OutputStream myOutPut;
    try {
        myInput = myContext.getAssets().open(DB_NAME);
        String outFileName =DB_PATH + DB_NAME;
        myOutPut = new FileOutputStream(outFileName);
        byte[] buffer = new byte[1024];
        int length;
        while((length = myInput.read(buffer))>0){
            myOutPut.write(buffer, 0, length);
        }
        myOutPut.flush();
        myOutPut.close();
        myInput.close();
        }
    catch (IOException e) {
        e.printStackTrace();
    }
} 

public SQLiteDatabase openDataBase() throws SQLException {
    String myPath = DB_PATH + DB_NAME;
    myDataBase = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READWRITE);      
    return myDataBase;
}

@Override
public synchronized void close() {
    if (myDataBase != null)
        myDataBase.close();
    super.close();
}

@Override
public void onCreate(SQLiteDatabase db) {

}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

}

}

这是我的SQLAccess类

public class SQLiteAccess {

private static SQLiteDatabase db;
static DBAdapter adb = null;

static VerbBean objVB = null;



public static String selectOneWordEnglish(String sql,
        Context mcontext) {
    String objVB = null;
    adb = DBAdapter.getDBAdapterInstance(mcontext);
    try {
        adb.createDataBase();

        db = adb.openDataBase();

        Cursor c = db.rawQuery(sql, null);
        c.moveToFirst();
        if (c.getCount() > 0) {
            for (int i = 0; i < c.getCount(); i++) {
                objVB = c.getString(c.getColumnIndex("english"));
                c.moveToNext();
            }
        }
    } catch (SQLException sqlEx) {
        sqlEx.printStackTrace();
    } catch (IOException ioEx) {
        ioEx.printStackTrace();
    }
    db.close();
    return objVB;
}



public static String selectOneValueSQL(String sql,Context mcontext)
{ 
    adb = DBAdapter.getDBAdapterInstance(mcontext);
    try {
        adb.createDataBase();

        db = adb.openDataBase();

        Cursor c = db.rawQuery(sql, null);
        c.moveToFirst();
        if (c.getCount() > 0) {
            for (int i = 0; i < c.getCount(); i++) {
                return c.getColumnName(c.getColumnIndex("SortOrder"));
            }
        }
    } catch (SQLException sqlEx) {
        sqlEx.printStackTrace();
    } catch (IOException ioEx) {
        ioEx.printStackTrace();
    }
    db.close();
  return ""; 
}

public static int selectCount(String sql, Context mContext)
{   int count = 0;
    adb = DBAdapter.getDBAdapterInstance(mContext);
    try {
        adb.createDataBase();

        db = adb.openDataBase();

        Cursor c = db.rawQuery(sql, null);
        count = c.getCount();

    } catch (SQLException sqlEx) {
        sqlEx.printStackTrace();
    } catch (IOException ioEx) {
        ioEx.printStackTrace();
}
db.close();
return count ;
}


public static int insertWithSQL(String sql,Context mContext)
{adb = DBAdapter.getDBAdapterInstance(mContext);
try {
    adb.createDataBase();

    db = adb.openDataBase();

    Cursor c = db.rawQuery(sql, null);

} catch (SQLException sqlEx) {
    sqlEx.printStackTrace();
} catch (IOException ioEx) {
    ioEx.printStackTrace();
}
db.close();
    return 0; 

}




public static Vector<VerbBean> selectAllFromDatabase(String sql,Context mContext)
{
    Vector<VerbBean> vecVB = new Vector<VerbBean>();
    VerbBean objVB;
    adb = DBAdapter.getDBAdapterInstance(mContext);
    try {
        adb.createDataBase();
        db = adb.openDataBase();
        Cursor c = db.rawQuery(sql, null);
        c.moveToFirst();
        if (c.getCount() > 0) {
            for (int i = 0; i < c.getCount(); i++) {
                objVB= new VerbBean();
                objVB.setVerbId(c.getString(c.getColumnIndex("VerbID")));
                objVB.setVerbName(c.getString(c.getColumnIndex("VerbName")));
                vecVB.addElement(objVB);
                c.moveToNext();
            }   
            c.moveToNext();
        }
    } catch (SQLException sqlEx) {
        sqlEx.printStackTrace();
    } catch (IOException ioEx) {
        ioEx.printStackTrace();
    }
    db.close();
    return vecVB;   
}

}

这就是我在Android设备(HTC Wildfire)中安装我的应用程序时的警告

05-04 12:07:14.033: WARN/System.err(2145): java.io.IOException
05-04 12:07:14.033: WARN/System.err(2145):     at android.content.res.AssetManager.readAsset(Native Method)
05-04 12:07:14.043: WARN/System.err(2145):     at android.content.res.AssetManager.access$700(AssetManager.java:36)
05-04 12:07:14.043: WARN/System.err(2145):     at android.content.res.AssetManager$AssetInputStream.read(AssetManager.java:571)
05-04 12:07:14.043: WARN/System.err(2145):     at com.lauruss.verb2verbe.DBAdapter.copyDataBase(DBAdapter.java:101)
05-04 12:07:14.043: WARN/System.err(2145):     at com.lauruss.verb2verbe.DBAdapter.createDataBase(DBAdapter.java:45)
05-04 12:07:14.043: WARN/System.err(2145):     at com.lauruss.verb2verbe.MainActivity.onCreate(MainActivity.java:52)
05-04 12:07:14.043: WARN/System.err(2145):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1065)
05-04 12:07:14.043: WARN/System.err(2145):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2745)
05-04 12:07:14.043: WARN/System.err(2145):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2797)
05-04 12:07:14.043: WARN/System.err(2145):     at android.app.ActivityThread.access$2300(ActivityThread.java:135)
05-04 12:07:14.043: WARN/System.err(2145):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2132)
05-04 12:07:14.053: WARN/System.err(2145):     at android.os.Handler.dispatchMessage(Handler.java:99)
05-04 12:07:14.053: WARN/System.err(2145):     at android.os.Looper.loop(Looper.java:143)
05-04 12:07:14.053: WARN/System.err(2145):     at android.app.ActivityThread.main(ActivityThread.java:4914)
05-04 12:07:14.053: WARN/System.err(2145):     at java.lang.reflect.Method.invokeNative(Native Method)
05-04 12:07:14.053: WARN/System.err(2145):     at java.lang.reflect.Method.invoke(Method.java:521)
05-04 12:07:14.063: WARN/System.err(2145):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
05-04 12:07:14.063: WARN/System.err(2145):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
05-04 12:07:14.063: WARN/System.err(2145):     at dalvik.system.NativeStart.main(Native Method)
05-04 12:07:14.143: INFO/Database(2145): sqlite returned: error code = 1, msg = no such table: tblVocab
05-04 12:07:14.153: WARN/System.err(2145): android.database.sqlite.SQLiteException: no such table: tblVocab: , while compiling: SELECT * FROM tblVocab
05-04 12:07:14.163: WARN/System.err(2145):     at android.database.sqlite.SQLiteCompiledSql.native_compile(Native Method)
05-04 12:07:14.163: WARN/System.err(2145):     at android.database.sqlite.SQLiteCompiledSql.compile(SQLiteCompiledSql.java:91)
05-04 12:07:14.163: WARN/System.err(2145):     at android.database.sqlite.SQLiteCompiledSql.<init>(SQLiteCompiledSql.java:64)
05-04 12:07:14.163: WARN/System.err(2145):     at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:80)
05-04 12:07:14.173: WARN/System.err(2145):     at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:46)
05-04 12:07:14.173: WARN/System.err(2145):     at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:53)
05-04 12:07:14.173: WARN/System.err(2145):     at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1417)
05-04 12:07:14.173: WARN/System.err(2145):     at android.database.sqlite.SQLiteDatabase.rawQuery(SQLiteDatabase.java:1387)
05-04 12:07:14.173: WARN/System.err(2145):     at com.lauruss.verb2verbe.SQLiteAccess.selectCount(SQLiteAccess.java:201)
05-04 12:07:14.173: WARN/System.err(2145):     at com.lauruss.verb2verbe.DataAccess.getRandomVocab(DataAccess.java:59)
05-04 12:07:14.183: WARN/System.err(2145):     at com.lauruss.verb2verbe.MainActivity.run(MainActivity.java:171)
05-04 12:07:14.183: WARN/System.err(2145):     at java.lang.Thread.run(Thread.java:1102)
05-04 12:07:14.193: WARN/dalvikvm(2145): threadid=7: thread exiting with uncaught exception (group=0x40028a00)
05-04 12:07:14.193: ERROR/AndroidRuntime(2145): FATAL EXCEPTION: Thread-8
05-04 12:07:14.193: ERROR/AndroidRuntime(2145): java.lang.IllegalArgumentException
05-04 12:07:14.193: ERROR/AndroidRuntime(2145):     at java.util.Random.nextInt(Random.java:225)
05-04 12:07:14.193: ERROR/AndroidRuntime(2145):     at com.lauruss.verb2verbe.DataAccess.getRandomVocab(DataAccess.java:64)
05-04 12:07:14.193: ERROR/AndroidRuntime(2145):     at com.lauruss.verb2verbe.MainActivity.run(MainActivity.java:171)
05-04 12:07:14.193: ERROR/AndroidRuntime(2145):     at java.lang.Thread.run(Thread.java:1102)
05-04 12:07:14.213: WARN/ActivityManager(102):   Force finishing activity com.lauruss.verb2verbe/.MainActivity

当我第一次连接设备并运行时,它会显示如下警告

05-04 12:38:01.983: WARN/System.err(2857): java.io.IOException
05-04 12:38:01.993: WARN/System.err(2857):     at android.content.res.AssetManager.readAsset(Native Method)
05-04 12:38:01.993: WARN/System.err(2857):     at android.content.res.AssetManager.access$700(AssetManager.java:36)
05-04 12:38:01.993: WARN/System.err(2857):     at android.content.res.AssetManager$AssetInputStream.read(AssetManager.java:571)
05-04 12:38:01.993: WARN/System.err(2857):     at com.lauruss.verb2verbe.DBAdapter.copyDataBase(DBAdapter.java:80)
05-04 12:38:01.993: WARN/System.err(2857):     at com.lauruss.verb2verbe.DBAdapter.createDataBase(DBAdapter.java:45)
05-04 12:38:01.993: WARN/System.err(2857):     at com.lauruss.verb2verbe.MainActivity.onCreate(MainActivity.java:52)
05-04 12:38:01.993: WARN/System.err(2857):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1065)
05-04 12:38:02.003: WARN/System.err(2857):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2745)
05-04 12:38:02.003: WARN/System.err(2857):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2797)
05-04 12:38:02.003: WARN/System.err(2857):     at android.app.ActivityThread.access$2300(ActivityThread.java:135)
05-04 12:38:02.003: WARN/System.err(2857):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2132)
05-04 12:38:02.003: WARN/System.err(2857):     at android.os.Handler.dispatchMessage(Handler.java:99)
05-04 12:38:02.003: WARN/System.err(2857):     at android.os.Looper.loop(Looper.java:143)
05-04 12:38:02.003: WARN/System.err(2857):     at android.app.ActivityThread.main(ActivityThread.java:4914)
05-04 12:38:02.003: WARN/System.err(2857):     at java.lang.reflect.Method.invokeNative(Native Method)
05-04 12:38:02.003: WARN/System.err(2857):     at java.lang.reflect.Method.invoke(Method.java:521)
05-04 12:38:02.013: WARN/System.err(2857):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
05-04 12:38:02.013: WARN/System.err(2857):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
05-04 12:38:02.013: WARN/System.err(2857):     at dalvik.system.NativeStart.main(Native Method)

2 个答案:

答案 0 :(得分:1)

尝试将数据库扩展名更改为.mp3,因为android对扩展名为.sqlite的数据库有大小限制。它允许最多4 MB的数据。

答案 1 :(得分:0)

此问题可能是您在加载到应用程序之前尝试打开数据库。在使用数据库之前,请始终牢记这些要点......

  1. 在创建数据库之前创建锁
  2. 向用户显示处理对话框
  3. 尝试使用单个跟踪路径
  4. &#34; / Android设备/数据/包/ DBNAME&#34; ==对于移动存储器

    &#34; / SD卡/ Android设备/数据/包/ DBNAME&#34; ==对于SdCard