上下文中的StackOverflowError

时间:2012-08-30 15:23:47

标签: android sqlite android-context sqliteopenhelper

我收到了“java.lang.StackOverflowError”错误。

这是代码:

08-30 10:34:49.528: E/AndroidRuntime(22897): FATAL EXCEPTION: main
08-30 10:34:49.528: E/AndroidRuntime(22897): java.lang.StackOverflowError
08-30 10:34:49.528: E/AndroidRuntime(22897):    at com.silm.database.ApprovedNotifications.<init>(ApprovedNotifications.java:27)
08-30 10:34:49.528: E/AndroidRuntime(22897):    at com.silm.database.RejectedNotifications.<init>(RejectedNotifications.java:27)
08-30 10:34:49.528: E/AndroidRuntime(22897):    at com.silm.database.ApprovedNotifications.<init>(ApprovedNotifications.java:28)
08-30 10:34:49.528: E/AndroidRuntime(22897):    at com.silm.database.RejectedNotifications.<init>(RejectedNotifications.java:27)
08-30 10:34:49.528: E/AndroidRuntime(22897):    at com.silm.database.ApprovedNotifications.<init>(ApprovedNotifications.java:28)
08-30 10:34:49.528: E/AndroidRuntime(22897):    at com.silm.database.RejectedNotifications.<init>(RejectedNotifications.java:27)
08-30 10:34:49.528: E/AndroidRuntime(22897):    at com.silm.database.ApprovedNotifications.<init>(ApprovedNotifications.java:28)
08-30 10:34:49.528: E/AndroidRuntime(22897):    at com.silm.database.RejectedNotifications.<init>(RejectedNotifications.java:27)
08-30 10:34:49.528: E/AndroidRuntime(22897):    at com.silm.database.ApprovedNotifications.<init>(ApprovedNotifications.java:28)
08-30 10:34:49.528: E/AndroidRuntime(22897):    at com.silm.database.RejectedNotifications.<init>(RejectedNotifications.java:27)
08-30 10:34:49.528: E/AndroidRuntime(22897):    at com.silm.database.ApprovedNotifications.<init>(ApprovedNotifications.java:28)
08-30 10:34:49.528: E/AndroidRuntime(22897):    at com.silm.database.RejectedNotifications.<init>(RejectedNotifications.java:27)
08-30 10:34:49.528: E/AndroidRuntime(22897):    at com.silm.database.ApprovedNotifications.<init>(ApprovedNotifications.java:28)
08-30 10:34:49.528: E/AndroidRuntime(22897):    at com.silm.database.RejectedNotifications.<init>(RejectedNotifications.java:27)
08-30 10:34:49.528: E/AndroidRuntime(22897):    at com.silm.database.ApprovedNotifications.<init>(ApprovedNotifications.java:28)
08-30 10:34:49.528: E/AndroidRuntime(22897):    at com.silm.database.RejectedNotifications.<init>(RejectedNotifications.java:27)
08-30 10:34:49.528: E/AndroidRuntime(22897):    at com.silm.database.ApprovedNotifications.<init>(ApprovedNotifications.java:28)
08-30 10:34:49.528: E/AndroidRuntime(22897):    at com.silm.database.RejectedNotifications.<init>(RejectedNotifications.java:27)
08-30 10:34:49.528: E/AndroidRuntime(22897):    at com.silm.database.ApprovedNotifications.<init>(ApprovedNotifications.java:28)
08-30 10:34:49.528: E/AndroidRuntime(22897):    at com.silm.database.RejectedNotifications.<init>(RejectedNotifications.java:27)
08-30 10:34:49.528: E/AndroidRuntime(22897):    at com.silm.database.ApprovedNotifications.<init>(ApprovedNotifications.java:28)
08-30 10:34:49.528: E/AndroidRuntime(22897):    at com.silm.database.RejectedNotifications.<init>(RejectedNotifications.java:27)
08-30 10:34:49.528: E/AndroidRuntime(22897):    at com.silm.database.ApprovedNotifications.<init>(ApprovedNotifications.java:28)
08-30 10:34:49.528: E/AndroidRuntime(22897):    at com.silm.database.RejectedNotifications.<init>(RejectedNotifications.java:27)
08-30 10:34:49.528: E/AndroidRuntime(22897):    at com.silm.database.ApprovedNotifications.<init>(ApprovedNotifications.java:28)
08-30 10:34:49.528: E/AndroidRuntime(22897):    at com.silm.database.RejectedNotifications.<init>(RejectedNotifications.java:27)
08-30 10:34:49.528: E/AndroidRuntime(22897):    at com.silm.database.ApprovedNotifications.<init>(ApprovedNotifications.java:28)
08-30 10:34:49.528: E/AndroidRuntime(22897):    at com.silm.database.RejectedNotifications.<init>(RejectedNotifications.java:27)
08-30 10:34:49.528: E/AndroidRuntime(22897):    at com.silm.database.ApprovedNotifications.<init>(ApprovedNotifications.java:28)
08-30 10:34:49.528: E/AndroidRuntime(22897):    at com.silm.database.RejectedNotifications.<init>(RejectedNotifications.java:27)
08-30 10:34:49.528: E/AndroidRuntime(22897):    at com.silm.database.ApprovedNotifications.<init>(ApprovedNotifications.java:28)
08-30 10:34:49.528: E/AndroidRuntime(22897):    at com.silm.database.RejectedNotifications.<init>(RejectedNotifications.java:27)
08-30 10:34:49.528: E/AndroidRuntime(22897):    at com.silm.database.ApprovedNotifications.<init>(ApprovedNotifications.java:28)
08-30 10:34:49.528: E/AndroidRuntime(22897):    at com.silm.database.RejectedNotifications.<init>(RejectedNotifications.java:27)
08-30 10:34:49.528: E/AndroidRuntime(22897):    at com.silm.database.ApprovedNotifications.<init>(ApprovedNotifications.java:28)
08-30 10:34:49.528: E/AndroidRuntime(22897):    at com.silm.database.RejectedNotifications.<init>(RejectedNotifications.java:27)
08-30 10:34:49.528: E/AndroidRuntime(22897):    at com.silm.database.ApprovedNotifications.<init>(ApprovedNotifications.java:28)
08-30 10:34:49.528: E/AndroidRuntime(22897):    at com.silm.database.RejectedNotifications.<init>(RejectedNotifications.java:27)
08-30 10:34:49.528: E/AndroidRuntime(22897):    at com.silm.database.ApprovedNotifications.<init>(ApprovedNotifications.java:28)
08-30 10:34:49.528: E/AndroidRuntime(22897):    at com.silm.database.RejectedNotifications.<init>(RejectedNotifications.java:27)
08-30 10:34:49.528: E/AndroidRuntime(22897):    at com.silm.database.ApprovedNotifications.<init>(ApprovedNotifications.java:28)
08-30 10:34:49.528: E/AndroidRuntime(22897):    at com.silm.database.RejectedNotifications.<init>(RejectedNotifications.java:27)
08-30 10:34:49.528: E/AndroidRuntime(22897):    at com.silm.database.ApprovedNotifications.<init>(ApprovedNotifications.java:28)
08-30 10:34:49.528: E/AndroidRuntime(22897):    at com.silm.database.RejectedNotifications.<init>(RejectedNotifications.java:27)
08-30 10:34:49.528: E/AndroidRuntime(22897):    at com.silm.database.ApprovedNotifications.<init>(ApprovedNotifications.java:28)
08-30 10:34:49.528: E/AndroidRuntime(22897):    at com.silm.database.RejectedNotifications.<init>(RejectedNotifications.java:27)
08-30 10:34:49.528: E/AndroidRuntime(22897):    at com.silm.database.ApprovedNotifications.<init>(ApprovedNotifications.java:28)
08-30 10:34:49.528: E/AndroidRuntime(22897):    at com.silm.database.RejectedNotifications.<init>(RejectedNotifications.java:27)
08-30 10:34:49.528: E/AndroidRuntime(22897):    at com.silm.database.ApprovedNotifications.<init>(ApprovedNotifications

关于项目:

我为什么要这样做:

我想点击批准按钮,将通知/消息保存到已批准通知/消息的数据库中。但我注意到,当我点击Aprrove按钮时,它被插入,当点击Rejected按钮时,它也被插入Rejected表中。所以,我检查是否在被拒绝的表中,然后将其插入到批准表中。一切正常,但是当我做同样的事情(从批准中删除通知并插入拒绝)时,我得到了StackOverFlow错误。

以下是生成错误的代码:

public RejectedNotifications(Context context) {
        super(context, TABLE_NAME_REJECTED_NOTIFICATION, null, DATABASE_VERSION);
        ***approved = new ApprovedNotifications(context);***
    }

表Approved的其他数据库处理程序与about code:

类似
public ApprovedNotifications(Context context) {
        super(context, TABLE_NAME_APPROVED_NOTIFICATION, null, DATABASE_VERSION);
        rejected = new RejectedNotifications(context);
    }

2 个答案:

答案 0 :(得分:3)

您已在代码中创建了无限循环。每次创建ApprovedNoficiations实例时,它都会创建一个RejectedNotifications实例,然后创建另一个ApprovedNotifications实例,依此类推......

这两个类之间的关系是循环的。如果您希望两个实例相互引用,则可能需要先创建一个实例,然后将其作为参数传递给第二个实例。

答案 1 :(得分:1)

你有一个非常明确的无限递归。 ApprovedNotifications的构造函数创建了一个新的RejectedNotificationsRejectedNotifications构造函数会创建一个新的ApprovedNotifications,依此类推。