0

我收到“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 的另一个数据库处理程序类似于关于代码:

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

2 回答 2

3

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

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

于 2012-08-30T15:26:45.823 回答
1

你有一个非常清晰的无限递归。ApprovedNotifications的构造函数创建一个新的RejectedNotifications. RejectedNotifications构造函数创建一个新ApprovedNotifications的,等等。

于 2012-08-30T15:28:07.383 回答