修复StackOverflow错误

时间:2011-07-15 10:03:22

标签: android

我开发了一个应用程序,其中有4个选项卡A,B,C,D。每个选项卡都包含一个活动。在第四个选项卡中,DI添加了一个ActivityGroup,其中我还有3个活动X,Y,Z。在我添加了活动组的第四个选项卡中,我收到了StackOverflow错误。请帮我修复此错误。< / p>

这是我的stackTrace: -

07-15 15:22:45.587: ERROR/AndroidRuntime(222): Uncaught handler: thread main exiting due to uncaught exception
07-15 15:22:45.677: ERROR/AndroidRuntime(222): java.lang.StackOverflowError
07-15 15:22:45.677: ERROR/AndroidRuntime(222):     at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1258)
07-15 15:22:45.677: ERROR/AndroidRuntime(222):     at android.view.ViewGroup.drawChild(ViewGroup.java:1529)
07-15 15:22:45.677: ERROR/AndroidRuntime(222):     at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1258)
07-15 15:22:45.677: ERROR/AndroidRuntime(222):     at android.view.ViewGroup.drawChild(ViewGroup.java:1529)
07-15 15:22:45.677: ERROR/AndroidRuntime(222):     at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1258)
07-15 15:22:45.677: ERROR/AndroidRuntime(222):     at android.view.ViewGroup.drawChild(ViewGroup.java:1529)
07-15 15:22:45.677: ERROR/AndroidRuntime(222):     at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1258)
07-15 15:22:45.677: ERROR/AndroidRuntime(222):     at android.view.ViewGroup.drawChild(ViewGroup.java:1529)
07-15 15:22:45.677: ERROR/AndroidRuntime(222):     at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1258)
07-15 15:22:45.677: ERROR/AndroidRuntime(222):     at android.view.ViewGroup.drawChild(ViewGroup.java:1529)
07-15 15:22:45.677: ERROR/AndroidRuntime(222):     at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1258)
07-15 15:22:45.677: ERROR/AndroidRuntime(222):     at android.widget.AbsListView.dispatchDraw(AbsListView.java:1324)
07-15 15:22:45.677: ERROR/AndroidRuntime(222):     at android.widget.ListView.dispatchDraw(ListView.java:2972)
07-15 15:22:45.677: ERROR/AndroidRuntime(222):     at android.view.View.draw(View.java:6641)
07-15 15:22:45.677: ERROR/AndroidRuntime(222):     at android.widget.AbsListView.draw(AbsListView.java:2171)
07-15 15:22:45.677: ERROR/AndroidRuntime(222):     at android.view.ViewGroup.drawChild(ViewGroup.java:1531)
07-15 15:22:45.677: ERROR/AndroidRuntime(222):     at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1258)
07-15 15:22:45.677: ERROR/AndroidRuntime(222):     at android.view.ViewGroup.drawChild(ViewGroup.java:1529)
07-15 15:22:45.677: ERROR/AndroidRuntime(222):     at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1258)
07-15 15:22:45.677: ERROR/AndroidRuntime(222):     at android.view.View.draw(View.java:6538)
07-15 15:22:45.677: ERROR/AndroidRuntime(222):     at android.widget.FrameLayout.draw(FrameLayout.java:352)
07-15 15:22:45.677: ERROR/AndroidRuntime(222):     at android.view.ViewGroup.drawChild(ViewGroup.java:1531)
07-15 15:22:45.677: ERROR/AndroidRuntime(222):     at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1258)
07-15 15:22:45.677: ERROR/AndroidRuntime(222):     at android.view.ViewGroup.drawChild(ViewGroup.java:1529)
07-15 15:22:45.677: ERROR/AndroidRuntime(222):     at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1258)
07-15 15:22:45.677: ERROR/AndroidRuntime(222):     at android.view.View.draw(View.java:6538)
07-15 15:22:45.677: ERROR/AndroidRuntime(222):     at android.widget.FrameLayout.draw(FrameLayout.java:352)
07-15 15:22:45.677: ERROR/AndroidRuntime(222):     at android.view.ViewGroup.drawChild(ViewGroup.java:1531)
07-15 15:22:45.677: ERROR/AndroidRuntime(222):     at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1258)
07-15 15:22:45.677: ERROR/AndroidRuntime(222):     at android.view.ViewGroup.drawChild(ViewGroup.java:1529)
07-15 15:22:45.677: ERROR/AndroidRuntime(222):     at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1258)
07-15 15:22:45.677: ERROR/AndroidRuntime(222):     at android.view.View.draw(View.java:6538)
07-15 15:22:45.677: ERROR/AndroidRuntime(222):     at android.widget.FrameLayout.draw(FrameLayout.java:352)
07-15 15:22:45.677: ERROR/AndroidRuntime(222):     at android.view.ViewGroup.drawChild(ViewGroup.java:1531)
07-15 15:22:45.677: ERROR/AndroidRuntime(222):     at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1258)
07-15 15:22:45.677: ERROR/AndroidRuntime(222):     at android.view.ViewGroup.drawChild(ViewGroup.java:1529)
07-15 15:22:45.677: ERROR/AndroidRuntime(222):     at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1258)
07-15 15:22:45.677: ERROR/AndroidRuntime(222):     at android.view.View.draw(View.java:6538)
07-15 15:22:45.677: ERROR/AndroidRuntime(222):     at android.widget.FrameLayout.draw(FrameLayout.java:352)
07-15 15:22:45.677: ERROR/AndroidRuntime(222):     at android.view.ViewGroup.drawChild(ViewGroup.java:1531)
07-15 15:22:45.677: ERROR/AndroidRuntime(222):     at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1258)
07-15 15:22:45.677: ERROR/AndroidRuntime(222):     at android.view.ViewGroup.drawChild(ViewGroup.java:1529)
07-15 15:22:45.677: ERROR/AndroidRuntime(222):     at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1258)
07-15 15:22:45.677: ERROR/AndroidRuntime(222):     at android.view.View.draw(View.java:6538)
07-15 15:22:45.677: ERROR/AndroidRuntime(222):     at android.widget.FrameLayout.draw(FrameLayout.java:352)
07-15 15:22:45.677: ERROR/AndroidRuntime(222):     at android.view.ViewGroup.drawChild(ViewGroup.java:1531)
07-15 15:22:45.677: ERROR/AndroidRuntime(222):     at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1258)
07-15 15:22:45.677: ERROR/AndroidRuntime(222):     at android.view.ViewGroup.drawChild(ViewGroup.java:1529)
07-15 15:22:45.677: ERROR/AndroidRuntime(222):     at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1258)
07-15 15:22:45.677: ERROR/AndroidRuntime(222):     at android.view.View.draw(View.java:6538)
07-15 15:22:45.677: ERROR/AndroidRuntime(222):     at android.widget.FrameLayout.draw(FrameLayout.java:352)
07-15 15:22:45.677: ERROR/AndroidRuntime(222):     at android.view.ViewGroup.drawChild(ViewGroup.java:1531)
07-15 15:22:45.677: ERROR/AndroidRuntime(222):     at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1258)
07-15 15:22:45.677: ERROR/AndroidRuntime(222):     at android.view.ViewGroup.drawChild(ViewGroup.java:1529)
07-15 15:22:45.677: ERROR/AndroidRuntime(222):     at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1258)
07-15 15:22:45.677: ERROR/AndroidRuntime(222):     at android.view.ViewGroup.drawChild(ViewGroup.java:1529)
07-15 15:22:45.677: ERROR/AndroidRuntime(222):     at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1258)
07-15 15:22:45.677: ERROR/AndroidRuntime(222):     at android.view.ViewGroup.drawChild(ViewGroup.java:1529)
07-15 15:22:45.677: ERROR/AndroidRuntime(222):     at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1258)
07-15 15:22:45.677: ERROR/AndroidRuntime(222):     at android.view.ViewGroup.drawChild(ViewGroup.java:1529)
07-15 15:22:45.677: ERROR/AndroidRuntime(222):     at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1258)
07-15 15:22:45.677: ERROR/AndroidRuntime(222):     at android.view.View.draw(View.java:6538)
07-15 15:22:45.677: ERROR/AndroidRuntime(222):     at android.widget.FrameLayout.draw(FrameLayout.java:352)
07-15 15:22:45.677: ERROR/AndroidRuntime(222):     at android.view.ViewGroup.drawChild(ViewGroup.java:1531)
07-15 15:22:45.677: ERROR/AndroidRuntime(222):     at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1258)
07-15 15:22:45.677: ERROR/AndroidRuntime(222):     at android.view.ViewGroup.drawChild(ViewGroup.java:1529)
07-15 15:22:45.677: ERROR/AndroidRuntime(222):     at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1258)
07-15 15:22:45.677: ERROR/AndroidRuntime(222):     at android.view.View.draw(View.java:6538)
07-15 15:22:45.677: ERROR/AndroidRuntime(222):     at android.widget.FrameLayout.draw(FrameLayout.java:352)
07-15 15:22:45.677: ERROR/AndroidRuntime(222):     at com.android.internal.policy.impl.PhoneWindow$DecorView.draw(PhoneWindow

请帮帮我...我对android很新。

3 个答案:

答案 0 :(得分:4)

我同意在大多数情况下还有另一种布局方式,但是如果你真的需要处理很多嵌套视图,那么就会有一个肮脏的解决方法。在您的视图子类中:

    private Handler mHandler = new Handler();

    @Override
    public void draw(Canvas canvas) {
            try {
                    super.draw(canvas);
            } catch (StackOverflowError e) {
                    mHandler.postDelayed(new Runnable() {
                            public void run() {
                                    invalidate();
                            }
                    }, 1);
            }
    }

它将停止当前调用堆栈,以防它失败并将当前视图的绘制延迟到不久的将来。并不是说它只是调用invalidate()(不需要使用postDelayed),但我仍然喜欢使用处理程序来创建延迟。

我仍然同意其他人的观点,即最佳解决方案是制作更好的布局。只有在没有其他方法可以进行布局时,才应使用此解决方法。

编辑:这种解决方法似乎也不起作用。似乎解决问题的唯一方法是减少视图层次结构的深度。

答案 1 :(得分:1)

我想你在某个地方有一个无限循环。

可能你已经在层次结构中的某个地方添加了两次相同的视图实例,现在你已经有了一个循环。

解决这个问题。并修复你的布局。我看不出有这么复杂布局的理由。

答案 2 :(得分:0)

我在谷歌搜索但无法找到解决方案。然后我想出了这个想法,它在我的旧三星Galaxy Nexus和Vodafone VF695上完美运行!

这是我在我的深层布局的根FrameLayout中的黑客攻击:

public class StackFrameLayout extends FrameLayout {
    private boolean stackOverflow;

    @Override
    public void draw(final Canvas canvas) {
        if (stackOverflow) {
            stackDraw(canvas);
        } else {
            try {
                super.draw(canvas);
            } catch (StackOverflowError e) {
                stackOverflow = true;
                stackDraw(canvas);
            }
        }
    }

    public void stackDraw(final Canvas canvas) {
        Thread thread = new Thread(getContext().getMainLooper().getThread().getThreadGroup(), new Runnable() {
            @Override
            public void run() {
                StackFrameLayout.super.draw(canvas);
            }
        }, "draw-thread", 2000000);
        thread.start();
        try {
            thread.join();
        } catch (InterruptedException e1) {
            e1.printStackTrace();
        }
    }
}

所以基本上我把复杂的绘图方法传递给一个堆栈大小更大的线程。因此,调用的方法堆栈将停止增加该点。然后UI线程等待绘图线程完成其工作,然后继续确保它顺序绘制,就像在UI线程上运行一样。