这是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)


我以前从未真正遇到过堆栈溢出错误!我不确定如何纠正此问题。我有一个单独的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);

最佳答案

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();
}


编辑:btw sqh不需要,因为此变量包含它位于相同的实例(非静态!),因此sqh == this ...

10-05 18:02