0

Android 开发和 Java 的新手,试图将我的查询分成查询/命令文件,这些文件继承自管理 QueryBase.java 文件的数据库。当我运行我的应用程序时,我收到以下错误:

08-28 09:31:02.266: I/dalvikvm(536): threadid=1: stack overflow on call to Landroid/database/sqlite/SQLiteOpenHelper;.<init>:VLLLI
08-28 09:31:02.266: I/dalvikvm(536): method requires 32+20+8=60 bytes, fp is 0x432d1318 (24 left)
08-28 09:31:02.266: I/dalvikvm(536): expanding stack end (0x432d1300 to 0x432d1000)
08-28 09:31:02.266: I/dalvikvm(536): Shrank stack (to 0x432d1300, curFrame is 0x432d3eb8)
08-28 09:31:02.266: D/AndroidRuntime(536): Shutting down VM
08-28 09:31:02.266: W/dalvikvm(536): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
08-28 09:31:02.356: D/dalvikvm(536): GC_FOR_MALLOC freed 4131 objects / 329392 bytes in 47ms
08-28 09:31:02.366: E/AndroidRuntime(536): FATAL EXCEPTION: main
08-28 09:31:02.366: E/AndroidRuntime(536): java.lang.StackOverflowError
08-28 09:31:02.366: E/AndroidRuntime(536): at com.childsoft.icantalk.queries.QueryBase.<init>(QueryBase.java:18)

我以前从未真正遇到过堆栈溢出错误!我不确定如何纠正这个 hrrrm。我有一个单独的 SchemaHelper 生成我的数据库 - 工作正常。我的 QueryBase.java 看起来像这样:

public class QueryBase extends SQLiteOpenHelper {

private static final String DATABASE_NAME = "icantalk.db";
private static final int DATABASE_VERSION = 1;

protected SQLiteDatabase sqdb;
protected QueryBase sqh;

public QueryBase(Context context) { 
    super(context, DATABASE_NAME, null, DATABASE_VERSION);  

    this.sqh = new QueryBase(context);
    this.sqdb = sqh.getWritableDatabase();
}

@Override
public void onCreate(SQLiteDatabase db) {           
}

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

}
}

QueryBase 上的第 18 行是这样的:

super(context, DATABASE_NAME, null, DATABASE_VERSION);

从 QueryBase 继承的示例命令文件如下所示:

public class ChildCommands extends QueryBase  {

public ChildCommands(Context context) {
    super(context);     
}

public long addChild(String name)
{
    ContentValues cv = new ContentValues();     
    cv.put(ChildrenTable.NAME, name);       
    SQLiteDatabase sd = super.getWritableDatabase();        
    long result = sd.insert(ChildrenTable.TABLE_NAME, ChildrenTable.NAME, cv);

    return result;
}   
}

对此方法的示例调用如下所示:

private ChildCommands command;
.....

this.command = new ChildCommands(this);
command.addChild(childsNameValue);
4

1 回答 1

2
public QueryBase(Context context) { 
    super(context, DATABASE_NAME, null, DATABASE_VERSION);  

    this.sqh = new QueryBase(context);
    this.sqdb = sqh.getWritableDatabase();
}

这导致在创建 QueryBase 的新对象的同时创建 QueryBase 的新对象。这是一个永无止境的电话。

用这个:

public QueryBase(Context context) { 
    super(context, DATABASE_NAME, null, DATABASE_VERSION);  

    this.sqh = this;
    this.sqdb = sqh.getWritableDatabase();
}

编辑:sqh不需要顺便说一句,因为这个变量包含它所在的相同实例(非静态!)所以sqh == this......

于 2012-08-28T09:54:16.523 回答