1

我在访问数据库时遇到问题。有趣的是它曾经运行良好,但最近它开始显示 SQLiteException “没有这样的表”。每次我启动应用程序时都不会发生这种情况,只是偶尔会发生,而且我看不到任何导致问题的特定设置。它总是与同一个类 RunForm 中的同一个表 form_data 相关。我在 adb 中检查了该表和数据库确实存在。这是我的 createDB 类公共类 createDB{

private static final String DATABASE_NAME = "mydatabase.db";
private static final String DB_PATH = "/data/data/com.mypackage/databases/";
private static final int DATABASE_VERSION = 2;

private OpenHelper openHelper;

private Context context;
private SQLiteDatabase db;

public createDB(Context ctx){
    try{
        this.context = ctx;

        openHelper = new OpenHelper(context);

        this.db = openHelper.getWritableDatabase();         
    }
    catch (IllegalStateException e){
        Log.e("ILLEGALstateEXC","CAUGHT");
    }   

}


public SQLiteDatabase openDataBase(){

    //Open the database
    String myPath = DB_PATH + DATABASE_NAME;
    try{
        db = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.CREATE_IF_NECESSARY);

    }catch(Exception e){
        Log.e("Opening database", e.toString());
    }

    return db;
}



public boolean checkDataBase(){      
    SQLiteDatabase checkDB = null; 
    try{
        String myPath = DB_PATH + DATABASE_NAME;
        checkDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);           
    }catch(SQLiteException e){
        //database does't exist yet.
        checkDB = null;
    }
    if(checkDB != null){
        checkDB.close(); 
    }
    return checkDB != null ? true : false;
}


public void close() 
{
     openHelper.close();
}


private static class OpenHelper extends SQLiteOpenHelper {
    OpenHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // TODO Auto-generated method stub

    }
}

除此之外,我还创建了两个类(dataDB 和 structureDB),它们包含对数据库特定表的操作(包括创建表)。它们基本上是相同的,所以我只展示了一个对导致问题的 form_data 表执行操作的。

public class dataDB{
private SQLiteDatabase db;
private structureDB structure_db;

private String TABLE_DATA = "form_data";
private String[] TABLE_DATA_FIELDS = {"id", "attr_id", "entity_id", "attr_value", "instance_number", "plot_number"};

private String TABLE_EXTRAS = "form_extras";
private String[] TABLE_EXTRAS_FIELDS = {"id", "name", "value", "instance_number", "plot_number"};

private String TABLE_VERSION = "form_version";
private String[] TABLE_VERSION_FIELDS = {"id", "name"};

public dataDB(SQLiteDatabase database){
    Log.e("DATA_DB","CONSTRUCTOR");
    this.db = dynamicTabs.db;//database;
    structure_db = new structureDB(this.db);
    this.createDataTables();
}

public void createDataTables(){
    db.execSQL(
            "create table if not exists " + TABLE_DATA + 
            " (" + TABLE_DATA_FIELDS[0] + " integer primary key autoincrement, "
            + TABLE_DATA_FIELDS[1] + " integer, "
            + TABLE_DATA_FIELDS[2] + " integer, "
            + TABLE_DATA_FIELDS[3] + " text, "
            + TABLE_DATA_FIELDS[4] + " integer, "
            + TABLE_DATA_FIELDS[5] + " integer "
            + ");"    );

    db.execSQL(
            "create table if not exists " + TABLE_EXTRAS + 
            " (" + TABLE_EXTRAS_FIELDS[0] + " integer primary key autoincrement, "
            + TABLE_EXTRAS_FIELDS[1] + " text, "
            + TABLE_EXTRAS_FIELDS[2] + " text, "
            + TABLE_EXTRAS_FIELDS[3] + " integer, "
            + TABLE_EXTRAS_FIELDS[4] + " integer "
            + ");"    );

    db.execSQL(
            "create table if not exists " + TABLE_VERSION + 
            " (" + TABLE_VERSION_FIELDS[0] + " integer primary key autoincrement, "
            + TABLE_VERSION_FIELDS[1] + " text "
            + ");"    );
}

public void recreateDataTables(){
    db.execSQL("drop table if exists " + TABLE_DATA);
    db.execSQL("drop table if exists " + TABLE_EXTRAS);
    db.execSQL("drop table if exists " + TABLE_VERSION);
    this.createDataTables();
}

public void saveVersion (String name){
    ContentValues initialValues = new ContentValues();
    initialValues.put(TABLE_VERSION_FIELDS[1], name);
    this.insertQuery(TABLE_VERSION, initialValues);
}

public String getVersionName(){
    String versionName = "";
    Cursor mCursor;
    mCursor = db.rawQuery("select * from " + TABLE_VERSION, null);
    if (mCursor.moveToFirst()){
        versionName = mCursor.getString(1);
    }   
    mCursor.close();
    return versionName;
}

public void saveExtraItem(String name, String value, int plotNo, int instanceNo){
    ContentValues initialValues = new ContentValues();
    initialValues.put(TABLE_EXTRAS_FIELDS[2], value);               
    if (this.itemExists(name, plotNo, instanceNo))
    {
        String condition = "" + TABLE_EXTRAS_FIELDS[4] + "='" + plotNo + "'"
        + " and " + TABLE_EXTRAS_FIELDS[1] + "='" + name + "'"
        + " and " + TABLE_EXTRAS_FIELDS[3] + "='" + instanceNo + "'";
        this.updateQuery(TABLE_EXTRAS, initialValues, condition);
    }
    else{
        initialValues.put(TABLE_EXTRAS_FIELDS[1], name);
        initialValues.put(TABLE_EXTRAS_FIELDS[3], instanceNo);
        initialValues.put(TABLE_EXTRAS_FIELDS[4], plotNo);          
        this.insertQuery(TABLE_EXTRAS, initialValues);
    }
}

public String getExtraItem(String name, int plotNo, int instanceNo){
    String itemValue = "";
    Cursor mCursor =  db.rawQuery("select * from " + TABLE_EXTRAS + 
            " where " + TABLE_EXTRAS_FIELDS[1] + "='" + name + "'" +
            " and " + TABLE_EXTRAS_FIELDS[3] + "='" + instanceNo + "'" +
            " and " + TABLE_EXTRAS_FIELDS[4] + "='" + plotNo + "'", null);
    if (mCursor.moveToFirst()){
        itemValue = mCursor.getString(mCursor.getColumnIndex(TABLE_EXTRAS_FIELDS[2]));
    }
    mCursor.close();
    return itemValue;
}   
public void cleanExtras(){
    db.execSQL("drop table if exists " + TABLE_EXTRAS);
    db.execSQL(
            "create table if not exists " + TABLE_EXTRAS + 
            " (" + TABLE_EXTRAS_FIELDS[0] + " integer primary key autoincrement, "
            + TABLE_EXTRAS_FIELDS[1] + " text, "
            + TABLE_EXTRAS_FIELDS[2] + " text, "
            + TABLE_EXTRAS_FIELDS[3] + " integer, "
            + TABLE_EXTRAS_FIELDS[4] + " integer "
            + ");"    );
}

public void saveItem(int structureIndex, int entityIndex, String value, int plotNo, int instanceNo){
    ContentValues initialValues = new ContentValues();
    initialValues.put(TABLE_DATA_FIELDS[3], value);             
    if (this.itemExists(structureIndex, plotNo, instanceNo))
    {
        String condition = "" + TABLE_DATA_FIELDS[5] + "='" + plotNo + "'"
        + " and " + TABLE_DATA_FIELDS[1] + "='" + structureIndex + "'"
        + " and " + TABLE_DATA_FIELDS[4] + "='" + instanceNo + "'";
        this.updateQuery(TABLE_DATA, initialValues, condition);
    }
    else{
        initialValues.put(TABLE_DATA_FIELDS[1], String.valueOf(structureIndex));
        initialValues.put(TABLE_DATA_FIELDS[2], entityIndex);
        initialValues.put(TABLE_DATA_FIELDS[4], instanceNo);
        initialValues.put(TABLE_DATA_FIELDS[5], plotNo);            
        this.insertQuery(TABLE_DATA, initialValues);
    }
}

public String loadItem(int structureIndex, int plotNo, int instanceNo){
    String itemValue = "";
    Cursor mCursor =  db.rawQuery("select * from " + TABLE_DATA + 
            " where " + TABLE_DATA_FIELDS[1] + "='" + structureIndex + "'" +
            " and " + TABLE_DATA_FIELDS[4] + "='" + instanceNo + "'" +
            " and " + TABLE_DATA_FIELDS[5] + "='" + plotNo + "'", null);
    if (mCursor.moveToFirst()){
        itemValue = mCursor.getString(mCursor.getColumnIndex(TABLE_DATA_FIELDS[3]));
    }
    mCursor.close();
    return itemValue;
}

public boolean itemExists(int structureIndex, int plotNo, int instanceNo){
    boolean exists = false;
    Cursor mCursor =  db.rawQuery("select count(*) from " + TABLE_DATA + 
            " where " + TABLE_DATA_FIELDS[5] + "='" + plotNo + "'"
            + " and " + TABLE_DATA_FIELDS[1] + "='" + structureIndex + "'"
            + " and " + TABLE_DATA_FIELDS[4] + "='" + instanceNo + "'", null);
    if (mCursor.moveToFirst()){
        exists = (mCursor.getInt(0)>0)?true:false;
    }
    mCursor.close();
    return exists;
}

public boolean itemExists(String name, int plotNo, int instanceNo){
    boolean exists = false;
    Cursor mCursor =  db.rawQuery("select count(*) from " + TABLE_EXTRAS + 
            " where " + TABLE_EXTRAS_FIELDS[4] + "='" + plotNo + "'"
            + " and " + TABLE_EXTRAS_FIELDS[1] + "='" + name + "'"
            + " and " + TABLE_EXTRAS_FIELDS[3] + "='" + instanceNo + "'", null);
    if (mCursor.moveToFirst()){
        exists = (mCursor.getInt(0)>0)?true:false;
    }
    mCursor.close();
    return exists;
}

private String getItemParent(int itemIndex){
    String itemName = "";
    Cursor mCursor =  db.rawQuery("select * from " + TABLE_DATA + " where " + TABLE_DATA_FIELDS[0] + "='" + itemIndex + "'", null);
    if (mCursor.moveToFirst()){
        itemName = structure_db.getFieldParent(mCursor.getInt(1));
    }
    mCursor.close();
    return itemName;
}   
public String getItemValue(int itemIndex){
    String itemValue = "";
    Cursor mCursor =  db.rawQuery("select * from " + TABLE_DATA + " where " + TABLE_DATA_FIELDS[0] + "='" + itemIndex + "'", null);
    if (mCursor.moveToFirst()){
        itemValue = mCursor.getString(3);
    }
    mCursor.close();
    return itemValue;

}

public String getItemType(int itemIndex){
    String itemType = "";
    int attributeID = -1;
    Cursor mCursor =  db.rawQuery("select * from " + TABLE_DATA + " where " + TABLE_DATA_FIELDS[0] + "='" + itemIndex + "'", null);
    if (mCursor.moveToFirst()){
        try{
            attributeID = mCursor.getInt(1);
            itemType = structure_db.getFieldType(attributeID);
        }catch (Exception e){

        }           
    }
    mCursor.close();
    return itemType;
}

public String getItemInstanceValue(int itemIndex, int instanceNo, int plotNo){
    String itemValue = "";
    Cursor mCursor =  db.rawQuery("select * from " + TABLE_DATA 
            + " where " + TABLE_DATA_FIELDS[1] + "='" + itemIndex + "'"
            + " and " + TABLE_DATA_FIELDS[4] + "='" + instanceNo + "'"
            + " and " + TABLE_DATA_FIELDS[5] + "='" + plotNo + "'", null);
    if (mCursor.moveToFirst()){
        itemValue = mCursor.getString(3);
    }
    mCursor.close();
    //Log.e("index"+itemIndex+"instance"+instanceNo, "plotNO"+plotNo+"value=="+itemValue+"===");
    return itemValue;
}

private String getItemPath(int itemIndex){
    String itemPath = "";
    Cursor mCursor =  db.rawQuery("select * from " + TABLE_DATA + " where " + TABLE_DATA_FIELDS[0] + "='" + itemIndex + "'", null);
    if (mCursor.moveToFirst()){
        itemPath = structure_db.getFieldPath(mCursor.getInt(1));
    }
    mCursor.close();
    return itemPath;
}

private String getItemPlot(int itemIndex){
    String itemPlot = "";
    Cursor mCursor =  db.rawQuery("select * from " + TABLE_DATA + " where " + TABLE_DATA_FIELDS[0] + "='" + itemIndex + "'", null);
    if (mCursor.moveToFirst()){
        itemPlot = mCursor.getString(5);
    }
    mCursor.close();
    return itemPlot;        
}

public String getItemInstanceNumber(int itemIndex){
    String itemInstanceNumber = "";
    Cursor mCursor =  db.rawQuery("select * from " + TABLE_DATA + " where " + TABLE_DATA_FIELDS[0] + "='" + itemIndex + "'", null);
    if (mCursor.moveToFirst()){
        itemInstanceNumber = mCursor.getString(4);
    }
    mCursor.close();
    return itemInstanceNumber;
}

public int getNumberOfInstances(int structureIndex, int plotNo){
    int instanceNumber = -1;
    Cursor mCursor =  db.rawQuery("select count(*) from " + TABLE_DATA              
            + " where " + TABLE_DATA_FIELDS[1] + "='" + structureIndex + "'"
            + " and " + TABLE_DATA_FIELDS[5] + "='" + plotNo + "'", null);      
    if (mCursor.moveToFirst()){
        instanceNumber = mCursor.getInt(0);
    }
    mCursor.close();
    return instanceNumber;
}

public String[] getItemData(int itemIndex){
    Cursor mCursor =  db.rawQuery("select id, attr_id from " + TABLE_DATA, null);
    if (mCursor.moveToFirst())
        for (int i=1;i<itemIndex;i++){
            mCursor.moveToNext();
        }
    itemIndex = mCursor.getInt(0);

    String[] itemData = new String[9];
    itemData[0] = this.getItemName(itemIndex);
    itemData[1] = this.getItemParent(itemIndex);
    itemData[2] = this.getItemGrandparent(itemIndex);
    itemData[3] = this.getItemValue(itemIndex);
    itemData[4] = this.getItemPath(itemIndex);
    itemData[5] = this.getItemPlot(itemIndex);
    itemData[6] = this.getItemInstanceNumber(itemIndex);
    itemData[7] = this.getItemType(itemIndex);
    itemData[8] = String.valueOf(mCursor.getInt(1));
    mCursor.close();
    return itemData;
}   
public void removePlotData(int plotNo){
    int maxPlotNumber = 0;
    db.delete(TABLE_DATA, TABLE_DATA_FIELDS[5]+"="+plotNo, null);
    Cursor mCursor =  db.rawQuery("select max("+TABLE_DATA_FIELDS[5]+") from " + TABLE_DATA, null);
    if (mCursor.moveToFirst()){
        maxPlotNumber = mCursor.getInt(0);
    }
    mCursor.close();
    ContentValues plotNumberValue;          
    for (int i=plotNo+1;i<=maxPlotNumber;i++){
        plotNumberValue = new ContentValues();
        plotNumberValue.put(TABLE_DATA_FIELDS[5],i-1);
        db.update(TABLE_DATA, plotNumberValue, TABLE_DATA_FIELDS[5] + "=" + i, null);           
    }       
}

private long insertQuery(String table, ContentValues values){
    return db.insert(table, null, values);
}

private long updateQuery(String table, ContentValues values, String whereClause){
    return db.update(table, values, whereClause, null);
}
}

然后我在跨应用程序的活动中使用它,如下所示:

createDB database = new createDB(this);
db = database.openDataBase();
dataDB data_db = new dataDB(db);

当我在 onCreate 方法中执行以下活动之一时显示错误

protected void onCreate(Bundle savedInstanceState) {
...
createDB database = new createDB(this);
db = database.openDataBase();
structureDB structure_db = new structureDB(db);
dataDB data_db = new dataDB(db);
Intent i = new Intent(dynamicTabs.this,RunForm.class);
...
}

并在 RunForm 活动的 onCreate 中

protected void onCreate(Bundle savedInstanceState) {
...
createDB database = new createDB(this);
db = database.openDataBase();
structureDB structure_db = new structureDB(db);
dataDB data_db = new dataDB(db);
this.structure_db.getFieldLabel(fieldID)
this.data_db.getItemInstanceValue(fieldID, 0, this.formPlotNo);
...
}

当我尝试从 form_data 表中选择值时,错误出现在代码的最后一行。使用 structureDB 类的早期查询工作正常。Logcat 显示如下:

   05-13 18:19:49.047: I/dAWF(20223): dynamicTabs:onCreate
05-13 18:19:49.566: D/dalvikvm(20223): GC freed 13081 objects / 937960 bytes in 69ms
05-13 18:19:49.566: I/dalvikvm(20223): Uncaught exception thrown by finalizer (will be discarded):
05-13 18:19:49.566: I/dalvikvm(20223): Ljava/lang/IllegalStateException;: Finalizing cursor android.database.sqlite.SQLiteCursor@44eafea8 on null that has not been deactivated or closed
05-13 18:19:49.566: I/dalvikvm(20223):  at android.database.sqlite.SQLiteCursor.finalize(SQLiteCursor.java:596)
05-13 18:19:49.566: I/dalvikvm(20223):  at dalvik.system.NativeStart.run(Native Method)
05-13 18:19:49.576: I/dalvikvm(20223): Uncaught exception thrown by finalizer (will be discarded):
05-13 18:19:49.576: I/dalvikvm(20223): Ljava/lang/IllegalStateException;: Finalizing cursor android.database.sqlite.SQLiteCursor@44efb6b8 on null that has not been deactivated or closed
05-13 18:19:49.586: I/dalvikvm(20223):  at android.database.sqlite.SQLiteCursor.finalize(SQLiteCursor.java:596)
05-13 18:19:49.586: I/dalvikvm(20223):  at dalvik.system.NativeStart.run(Native Method)
05-13 18:19:49.626: E/Database(20223): Leak found
05-13 18:19:49.626: E/Database(20223): java.lang.IllegalStateException: /data/data/com.mypackage/databases/mydatabase.db SQLiteDatabase created and never closed
05-13 18:19:49.626: E/Database(20223):  at android.database.sqlite.SQLiteDatabase.<init>(SQLiteDatabase.java:1695)
05-13 18:19:49.626: E/Database(20223):  at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:739)
05-13 18:19:49.626: E/Database(20223):  at com.mypackage.ddatabase.createDB.openDataBase(createDB.java:45)
05-13 18:19:49.626: E/Database(20223):  at com.mypackage.dform.RunForm.onCreate(RunForm.java:70)
05-13 18:19:49.626: E/Database(20223):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
05-13 18:19:49.626: E/Database(20223):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2459)
05-13 18:19:49.626: E/Database(20223):  at android.app.ActivityThread.startActivityNow(ActivityThread.java:2335)
05-13 18:19:49.626: E/Database(20223):  at android.app.LocalActivityManager.moveToState(LocalActivityManager.java:127)
05-13 18:19:49.626: E/Database(20223):  at android.app.LocalActivityManager.startActivity(LocalActivityManager.java:339)
05-13 18:19:49.626: E/Database(20223):  at android.widget.TabHost$IntentContentStrategy.getContentView(TabHost.java:648)
05-13 18:19:49.626: E/Database(20223):  at android.widget.TabHost.setCurrentTab(TabHost.java:320)
05-13 18:19:49.626: E/Database(20223):  at android.widget.TabHost.addTab(TabHost.java:213)
05-13 18:19:49.626: E/Database(20223):  at com.mypackage.dawf.plotDynamicTabs.onCreate(plotDynamicTabs.java:108)
05-13 18:19:49.626: E/Database(20223):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
05-13 18:19:49.626: E/Database(20223):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2459)
05-13 18:19:49.626: E/Database(20223):  at android.app.ActivityThread.startActivityNow(ActivityThread.java:2335)
05-13 18:19:49.626: E/Database(20223):  at android.app.LocalActivityManager.moveToState(LocalActivityManager.java:127)
05-13 18:19:49.626: E/Database(20223):  at android.app.LocalActivityManager.startActivity(LocalActivityManager.java:339)
05-13 18:19:49.626: E/Database(20223):  at android.widget.TabHost$IntentContentStrategy.getContentView(TabHost.java:648)
05-13 18:19:49.626: E/Database(20223):  at android.widget.TabHost.setCurrentTab(TabHost.java:320)
05-13 18:19:49.626: E/Database(20223):  at android.widget.TabHost.addTab(TabHost.java:213)
05-13 18:19:49.626: E/Database(20223):  at com.mypackage.dawf.dynamicTabs.onCreate(dynamicTabs.java:157)
05-13 18:19:49.626: E/Database(20223):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
05-13 18:19:49.626: E/Database(20223):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2459)
05-13 18:19:49.626: E/Database(20223):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512)
05-13 18:19:49.626: E/Database(20223):  at android.app.ActivityThread.access$2200(ActivityThread.java:119)
05-13 18:19:49.626: E/Database(20223):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1863)
05-13 18:19:49.626: E/Database(20223):  at android.os.Handler.dispatchMessage(Handler.java:99)
05-13 18:19:49.626: E/Database(20223):  at android.os.Looper.loop(Looper.java:123)
05-13 18:19:49.626: E/Database(20223):  at android.app.ActivityThread.main(ActivityThread.java:4363)
05-13 18:19:49.626: E/Database(20223):  at java.lang.reflect.Method.invokeNative(Native Method)
05-13 18:19:49.626: E/Database(20223):  at java.lang.reflect.Method.invoke(Method.java:521)
05-13 18:19:49.626: E/Database(20223):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
05-13 18:19:49.626: E/Database(20223):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
05-13 18:19:49.626: E/Database(20223):  at dalvik.system.NativeStart.main(Native Method)
05-13 18:19:49.686: E/Database(20223): Leak found
05-13 18:19:49.686: E/Database(20223): java.lang.IllegalStateException: /data/data/com.mypackage.dynamicAWF/databases/mydatabase.db SQLiteDatabase created and never closed
05-13 18:19:49.686: E/Database(20223):  at android.database.sqlite.SQLiteDatabase.<init>(SQLiteDatabase.java:1695)
05-13 18:19:49.686: E/Database(20223):  at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:739)
05-13 18:19:49.686: E/Database(20223):  at com.mypackage.ddatabase.createDB.openDataBase(createDB.java:45)
05-13 18:19:49.686: E/Database(20223):  at com.mypackage.dawf.plotDynamicTabs.onCreate(plotDynamicTabs.java:80)
05-13 18:19:49.686: E/Database(20223):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
05-13 18:19:49.686: E/Database(20223):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2459)
05-13 18:19:49.686: E/Database(20223):  at android.app.ActivityThread.startActivityNow(ActivityThread.java:2335)
05-13 18:19:49.686: E/Database(20223):  at android.app.LocalActivityManager.moveToState(LocalActivityManager.java:127)
05-13 18:19:49.686: E/Database(20223):  at android.app.LocalActivityManager.startActivity(LocalActivityManager.java:339)
05-13 18:19:49.686: E/Database(20223):  at android.widget.TabHost$IntentContentStrategy.getContentView(TabHost.java:648)
05-13 18:19:49.686: E/Database(20223):  at android.widget.TabHost.setCurrentTab(TabHost.java:320)
05-13 18:19:49.686: E/Database(20223):  at android.widget.TabHost.addTab(TabHost.java:213)
05-13 18:19:49.686: E/Database(20223):  at com.mypackage.dawf.dynamicTabs.onCreate(dynamicTabs.java:157)
05-13 18:19:49.686: E/Database(20223):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
05-13 18:19:49.686: E/Database(20223):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2459)
05-13 18:19:49.686: E/Database(20223):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512)
05-13 18:19:49.686: E/Database(20223):  at android.app.ActivityThread.access$2200(ActivityThread.java:119)
05-13 18:19:49.686: E/Database(20223):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1863)
05-13 18:19:49.686: E/Database(20223):  at android.os.Handler.dispatchMessage(Handler.java:99)
05-13 18:19:49.686: E/Database(20223):  at android.os.Looper.loop(Looper.java:123)
05-13 18:19:49.686: E/Database(20223):  at android.app.ActivityThread.main(ActivityThread.java:4363)
05-13 18:19:49.686: E/Database(20223):  at java.lang.reflect.Method.invokeNative(Native Method)
05-13 18:19:49.686: E/Database(20223):  at java.lang.reflect.Method.invoke(Method.java:521)
05-13 18:19:49.686: E/Database(20223):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
05-13 18:19:49.686: E/Database(20223):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
05-13 18:19:49.686: E/Database(20223):  at dalvik.system.NativeStart.main(Native Method)
05-13 18:19:49.717: E/Database(20223): Leak found
05-13 18:19:49.717: E/Database(20223): java.lang.IllegalStateException: /data/data/com.mypackage.dynamicAWF/databases/mydatabase.db SQLiteDatabase created and never closed
05-13 18:19:49.717: E/Database(20223):  at android.database.sqlite.SQLiteDatabase.<init>(SQLiteDatabase.java:1695)
05-13 18:19:49.717: E/Database(20223):  at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:739)
05-13 18:19:49.717: E/Database(20223):  at com.mypackage.ddatabase.createDB.openDataBase(createDB.java:45)
05-13 18:19:49.717: E/Database(20223):  at com.mypackage.dawf.dynamicTabs.onCreate(dynamicTabs.java:100)
05-13 18:19:49.717: E/Database(20223):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
05-13 18:19:49.717: E/Database(20223):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2459)
05-13 18:19:49.717: E/Database(20223):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512)
05-13 18:19:49.717: E/Database(20223):  at android.app.ActivityThread.access$2200(ActivityThread.java:119)
05-13 18:19:49.717: E/Database(20223):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1863)
05-13 18:19:49.717: E/Database(20223):  at android.os.Handler.dispatchMessage(Handler.java:99)
05-13 18:19:49.717: E/Database(20223):  at android.os.Looper.loop(Looper.java:123)
05-13 18:19:49.717: E/Database(20223):  at android.app.ActivityThread.main(ActivityThread.java:4363)
05-13 18:19:49.717: E/Database(20223):  at java.lang.reflect.Method.invokeNative(Native Method)
05-13 18:19:49.717: E/Database(20223):  at java.lang.reflect.Method.invoke(Method.java:521)
05-13 18:19:49.717: E/Database(20223):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
05-13 18:19:49.717: E/Database(20223):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
05-13 18:19:49.717: E/Database(20223):  at dalvik.system.NativeStart.main(Native Method)
05-13 18:19:49.776: I/fileVERSIONname(20223): ==0.1
05-13 18:19:49.787: I/dbVERSIONname(20223): ==0.1
05-13 18:19:49.796: I/dAWF(20223): plotDynamicTabs:onCreate
05-13 18:19:49.897: E/currPLOTNo(20223): ==0
05-13 18:19:49.907: I/dAWF(20223): RunForm:onCreate
05-13 18:19:49.977: E/FORMplotNO(20223): ==0
05-13 18:19:50.097: E/currPLOTN1111(20223): ==0
05-13 18:19:50.107: I/dAWF(20223): RunForm:onResume
05-13 18:19:50.117: I/dAWF(20223): plotDynamicTabs:onResume
05-13 18:19:50.117: I/dAWF(20223): dynamicTabs:onResume
05-13 18:19:50.237: E/com.mypackage.dform.RunForm(20223): Error occurred in ProcessForm:no such table: form_data
05-13 18:19:50.237: W/System.err(20223): android.database.sqlite.SQLiteException: no such table: form_data
05-13 18:19:50.237: W/System.err(20223):    at android.database.sqlite.SQLiteQuery.native_fill_window(Native Method)
05-13 18:19:50.237: W/System.err(20223):    at android.database.sqlite.SQLiteQuery.fillWindow(SQLiteQuery.java:75)
05-13 18:19:50.237: W/System.err(20223):    at android.database.sqlite.SQLiteCursor.fillWindow(SQLiteCursor.java:288)
05-13 18:19:50.247: W/System.err(20223):    at android.database.sqlite.SQLiteCursor.getCount(SQLiteCursor.java:269)
05-13 18:19:50.247: W/System.err(20223):    at android.database.AbstractCursor.moveToPosition(AbstractCursor.java:171)
05-13 18:19:50.247: W/System.err(20223):    at android.database.AbstractCursor.moveToFirst(AbstractCursor.java:248)
05-13 18:19:50.247: W/System.err(20223):    at com.mypackage.ddatabase.dataDB.getItemInstanceValue(dataDB.java:264)
05-13 18:19:50.247: W/System.err(20223):    at com.mypackage.dform.RunForm.getFormData(RunForm.java:530)
05-13 18:19:50.247: W/System.err(20223):    at com.mypackage.dform.RunForm.run(RunForm.java:563)
05-13 18:19:50.247: W/System.err(20223):    at java.lang.Thread.run(Thread.java:1096)
05-13 18:19:50.807: I/ActivityManager(52): Displayed activity com.mypackage.dynamicAWF/com.mypackage.dawf.dynamicTabs: 1795 ms (total 1795 ms)
05-13 18:19:51.147: E/gralloc(52): [unregister] handle 0x4fe5b0 still locked (state=40000001)
05-13 18:19:56.057: D/dalvikvm(104): GC freed 334 objects / 15920 bytes in 150ms

我做了一些更改并摆脱了 IllegalStateException,但问题仍然存在,日志看起来像这样(数据库之前已经成功读取了很多次,但 form_data 表仍然会导致问题):

05-14 15:52:28.345: I/dAWF(455): dynamicTabs:onCreate
05-14 15:52:28.715: I/fileVERSIONname(455): ==0.1
05-14 15:52:28.725: I/dbVERSIONname(455): ==0.1
05-14 15:52:28.755: I/dAWF(455): plotDynamicTabs:onCreate
05-14 15:52:28.895: D/dalvikvm(455): GC freed 5524 objects / 349592 bytes in 68ms
05-14 15:52:28.935: I/dAWF(455): RunForm:onCreate
05-14 15:52:29.195: I/dAWF(455): RunForm:onResume
05-14 15:52:29.205: I/dAWF(455): plotDynamicTabs:onResume
05-14 15:52:29.205: I/dAWF(455): dynamicTabs:onResume
05-14 15:52:29.335: E/com.mypackage.android.dform.RunForm(455): Error occurred in ProcessForm:no such table: form_data
05-14 15:52:29.345: W/System.err(455): android.database.sqlite.SQLiteException: no such table: form_data
05-14 15:52:29.345: W/System.err(455):  at android.database.sqlite.SQLiteQuery.native_fill_window(Native Method)
05-14 15:52:29.345: W/System.err(455):  at android.database.sqlite.SQLiteQuery.fillWindow(SQLiteQuery.java:75)
05-14 15:52:29.345: W/System.err(455):  at android.database.sqlite.SQLiteCursor.fillWindow(SQLiteCursor.java:288)
05-14 15:52:29.345: W/System.err(455):  at android.database.sqlite.SQLiteCursor.getCount(SQLiteCursor.java:269)
05-14 15:52:29.345: W/System.err(455):  at android.database.AbstractCursor.moveToPosition(AbstractCursor.java:171)
05-14 15:52:29.345: W/System.err(455):  at android.database.AbstractCursor.moveToFirst(AbstractCursor.java:248)
05-14 15:52:29.345: W/System.err(455):  at com.mypackage.android.ddatabase.dataDB.getItemInstanceValue(dataDB.java:264)
05-14 15:52:29.345: W/System.err(455):  at com.mypackage.android.dform.RunForm.getFormData(RunForm.java:529)
05-14 15:52:29.345: W/System.err(455):  at com.mypackage.and
4

1 回答 1

0

查看Finalizing a Cursor 尚未停用或关闭的非致命错误

看看你的例外。它与游标管理有关。尝试使用 startManagingCursor。

于 2012-05-13T19:59:42.367 回答