我有关于单例课程的问题。当我调用next()方法时,我希望mProcessIndex会增加,但实际上不会,因此不会导致stackoverflow错误。那么,问题是修改mProcessIndex值的正确方法是什么?
代码如下:
public class HomePopupDisplayManager {
private static HomePopupDisplayManager sInstance;
private List<WeakReference<HomePopupMessage>> mMessages;
private int mProcessIndex;
private HomePopupDisplayManager() {
mMessages = new ArrayList<>();
mProcessIndex = 0;
}
public static synchronized HomePopupDisplayManager getInstance() {
if (sInstance == null) {
sInstance = new HomePopupDisplayManager();
}
return sInstance;
}
public void register(@NonNull HomePopupMessage message, @IntRange(from = 0) int order) {
mMessages.add(order, new WeakReference<>(message));
}
public void next() {
if (mProcessIndex >= 0 && mProcessIndex < mMessages.size()) {
HomePopupMessage message = mMessages.get(mProcessIndex).get();
if (message != null) {
next();
mProcessIndex++;
}
}
}
}
最佳答案
问题出在:
next();
mProcessIndex++;
应该:
mProcessIndex++;
next();