1

I have developed an application in which i have 4 tabs A,B,C,D. Each of the Tab contains an Activity. In the fourth tab D I have added an ActivityGroup in which I am having 3 more Activities X,Y,Z.In the fourth Tab where I have added the activity group I am getting StackOverflow error.Please help me out in fixing this error.

this is my 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

Please help me out ...I am very new to android.

Rasel
  • 15,499
  • 6
  • 40
  • 50
prachi sharma
  • 121
  • 1
  • 2
  • 5
  • Post your code. This stacktrace is just not readable – Andrew Anderson Jul 15 '11 at 10:06
  • 1
    Without seeing the code that is causing the error it's really hard to know what goes wrong, but it seems like you end up with a memory-consuming recursion somewhere. – Stefan H Singer Jul 15 '11 at 10:07
  • post your code instead of stacktrace – Randroid Jul 15 '11 at 10:08
  • 1
    posting code is a bit difficult because it has many activities.In my application I am having 4 tabs containing Activities names as A,B,C,D. In the 4th tab this exception is thrown where it contains 3 more sub-Activities called D1,D2,D3. From the Tab D I can Navigate in this way.... D-> D1, D->D2, D2->D3 and then back from D3 to D.After a certain number of operations it's throwing the stackOverflow exception. I am not able to make out the reason why am I getting the error. – prachi sharma Jul 15 '11 at 10:14
  • flatten your view hierarchy by replacing your layouts with relative. Series of articles that Romain suggested here : android-developers.blogspot.com/search/label/Optimization – Randroid Jul 15 '11 at 10:20
  • Hi raghu! I tried several ways to simplyfy the layouts but the problem still persists. I tried to use popups instead of activity but still after certain number of navigations it's throwing this exception..please help me out to fix this.. – prachi sharma Jul 15 '11 at 10:23
  • Concentrate on this error which Is the first line of ur stacktrace 07-15 15:22:45.587: ERROR/AndroidRuntime(222): Uncaught handler: thread main exiting due to uncaught exception. with out seeing ur code I cant help and checkout this, android-developers.blogspot.com/search/label/Optimization – Randroid Jul 15 '11 at 10:40
  • Hi, StackOverflow error on Stackoverflow.com interesting.... :P This error in java is usually thrown because of deep recursion. Check ur code once.. http://download.oracle.com/javase/1.4.2/docs/api/java/lang/StackOverflowError.html – om252345 Jul 15 '11 at 10:50

2 Answers2

1

I suppose you have an infinite loop somewhere.

Probably you've added the same instance of a view twice somewhere in your hierarchy and now you've got a cycle.

Fix that. And fix your layout. I can't see a reason to have such a complex layout.

Pedro Loureiro
  • 11,436
  • 2
  • 31
  • 37
  • Hi Pedro loureiro!! I think you got my error very corectly. Can u please tell me how to trace that where I am using the same instance of a view.And 1 more thing I wanted to ask which FLAG CONSTANTS should we use with the Intent to replace the contentView of the Tab with a new view???+ – prachi sharma Jul 15 '11 at 11:06
  • never add a view by code unless you have created it in that moment... and don't add it twice! (Never do findViewById and then add the returned view somewhere else). I can't answer the other question. – Pedro Loureiro Jul 15 '11 at 12:46
  • It's not possible to add a view twice, it immediately throws an exception mentioning the view is already attached. So that can't be the reason for the stack overflow. – 3c71 Jun 17 '14 at 09:23
0

I have search all over the Google but couldn't find a solution. Then I have just came up with this idea and it works perfectly on my old Samsung Galaxy Nexus and the Vodafone VF695!

Here is my hack in the root FrameLayout of my very deep layout:

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

So basically I passed the complicated drawing methods into a thread with larger stack size. So the stack of the methods called will stop increasing a that point. Then the UI thread wait for the drawing thread to finish its work and then continue to ensure it draw sequentially like running on the UI thread.

Chris
  • 704
  • 7
  • 10