Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to debug (this?) StackOverflowError with a finite logcat?

I'm sorry if this is broad, inappropriate or a dupe, feel free to flag.

However I couldn't find a solution. I'm getting StackOverflowError, and Android Studio shows -

08-03 16:22:49.293    5163-5163/com.package E/AndroidRuntime﹕ FATAL EXCEPTION: main
    java.lang.StackOverflowError
            at android.view.ViewGroup.jumpDrawablesToCurrentState(ViewGroup.java:5384)
            at android.view.ViewGroup.jumpDrawablesToCurrentState(ViewGroup.java:5388)
            at android.view.ViewGroup.jumpDrawablesToCurrentState(ViewGroup.java:5388)
            at android.view.ViewGroup.jumpDrawablesToCurrentState(ViewGroup.java:5388)
            at android.view.ViewGroup.jumpDrawablesToCurrentState(ViewGroup.java:5388)
            at android.view.ViewGroup.jumpDrawablesToCurrentState(ViewGroup.java:5388)
            at android.view.ViewGroup.jumpDrawablesToCurrentState(ViewGroup.java:5388)
            at android.view.ViewGroup.jumpDrawablesToCurrentState(ViewGroup.java:5388)
            at android.view.ViewGroup.jumpDrawablesToCurrentState(ViewGroup.java:5388)
            at android.view.ViewGroup.jumpDrawablesToCurrentState(ViewGroup.java:5388)
            at android.view.ViewGroup.jumpDrawablesToCurrentState(ViewGroup.java:5388)
            at android.view.ViewGroup.jumpDrawablesToCurrentState(ViewGroup.java:5388)
            at android.view.ViewGroup.jumpDrawablesToCurrentState(ViewGroup.java:5388)
            at android.view.ViewGroup.jumpDrawablesToCurrentState(ViewGroup.java:5388)
            at android.view.ViewGroup.jumpDrawablesToCurrentState(ViewGroup.java:5388)
            at android.view.ViewGroup.jumpDrawablesToCurrentState(ViewGroup.java:5388)
            at android.view.ViewGroup.jumpDrawablesToCurrentState(ViewGroup.java:5388)
            at android.view.ViewGroup.jumpDrawablesToCurrentState(ViewGroup.java:5388)
            at android.view.ViewGroup.jumpDrawablesToCurrentState(ViewGroup.java:5388)
            at android.view.ViewGroup.jumpDrawablesToCurrentState(ViewGroup.java:5388)
            at android.view.ViewGroup.jumpDrawablesToCurrentState(ViewGroup.java:5388)
            at android.view.ViewGroup.jumpDrawablesToCurrentState(ViewGroup.java:5388)
            at android.view.ViewGroup.jumpDrawablesToCurrentState(ViewGroup.java:5388)
            at android.view.ViewGroup.jumpDrawablesToCurrentState(ViewGroup.java:5388)
            at android.view.ViewGroup.jumpDrawablesToCurrentState(ViewGroup.java:5388)
            at android.view.ViewGroup.jumpDrawablesToCurrentState(ViewGroup.java:5388)
            at android.view.ViewGroup.jumpDrawablesToCurrentState(ViewGroup.java:5388)
            at android.view.ViewGroup.jumpDrawablesToCurrentState(ViewGroup.java:5388)
            at android.view.ViewGroup.jumpDrawablesToCurrentState(ViewGroup.java:5388)
            at android.view.ViewGroup.jumpDrawablesToCurrentState(ViewGroup.java:5388)
            at android.view.ViewGroup.jumpDrawablesToCurrentState(ViewGroup.java:5388)
            at android.view.ViewGroup.jumpDrawablesToCurrentState(ViewGroup.java:5388)
            at android.view.ViewGroup.jumpDrawablesToCurrentState(ViewGroup.java:5388)
            at android.view.ViewGroup.jumpDrawablesToCurrentState(ViewGroup.java:5388)
            at android.view.ViewGroup.jumpDrawablesToCurrentState(ViewGroup.java:5388)
            at android.view.ViewGroup.jumpDrawablesToCurrentState(ViewGroup.java:5388)
            at android.view.ViewGroup.jumpDrawablesToCurrentState(ViewGroup.java:5388)
            at android.view.ViewGroup.jumpDrawablesToCurrentState(ViewGroup.java:5388)
            at android.view.ViewGroup.jumpDrawablesToCurrentState(ViewGroup.java:5388)
            at android.view.ViewGroup.jumpDrawablesToCurrentState(ViewGroup.java:5388)
            at android.view.ViewGroup.jumpDrawablesToCurrentState(ViewGroup.java:5388)
            at android.view.ViewGroup.jumpDrawablesToCurrentState(ViewGroup.java:5388)
            at android.view.ViewGroup.jumpDrawablesToCurrentState(ViewGroup.java:5388)
            at android.view.ViewGroup.jumpDrawablesToCurrentState(ViewGroup.java:5388)
            at android.view.ViewGroup.jumpDrawablesToCurrentState(ViewGroup.java:5388)
            at android.view.ViewGroup.jumpDrawablesToCurrentState(ViewGroup.java:5388)
            at android.view.ViewGroup.jumpDrawablesToCurrentState(ViewGroup.java:5388)
            at android.view.ViewGroup.jumpDrawablesToCurrentState(ViewGroup.java:5388)
            at android.view.ViewGroup.jumpDrawablesToCurrentState(ViewGroup.java:5388)
            at android.view.ViewGroup.jumpDrawablesToCurrentState(ViewGroup.java:5388)
            at android.view.ViewGroup.jumpDrawablesToCurrentState(ViewGroup.java:5388)
            at android.view.ViewGroup.jumpDrawablesToCurrentState(ViewGroup.java:5388)
            at android.view.ViewGroup.jumpDrawablesToCurrentState(V

I spent hours right now trying to understand what's wrong: I'm not aware of any recursive call in my code. I'm asking:

  • Do you have any suggestion about what could be causing this particular issue?
  • Do you have any suggestion about how to technically face similar issues? Any way to get to the "root" cause in the stack? There should be some recursive stuff going on, but it surely has (a) a starting point, and a call to that point; (b) an end, when the StackOverflowError is thrown. We can see the end part, but not the start, because the logcat stops.
like image 539
natario Avatar asked Aug 03 '15 14:08

natario


People also ask

How do I debug StackOverflowError?

The simplest solution is to carefully inspect the stack trace and detect the repeating pattern of line numbers. These line numbers indicate the code being recursively called. Once you detect these lines, you must carefully inspect your code and understand why the recursion never terminates.

How do I fix Exception in thread main Java Lang StackOverflowError?

Increase Thread Stack Size (-Xss) lang. StackOverflowError , the thread stack size can be increased to allow a larger number of invocations. Increasing the stack size can be useful, for example, when the program involves calling a large number of methods or using lots of local variables.

What causes Java Lang StackOverflowError?

The error java. lang. StackOverflowError is thrown to indicate that the application's stack was exhausted, due to deep recursion i.e your program/script recurses too deeply.

Can you catch StackOverflowError Java?

StackOverflowError is an error which Java doesn't allow to catch, for instance, stack running out of space, as it's one of the most common runtime errors one can encounter.


1 Answers

I had this issue today working with fragments. This code was sufficient to cause the error:

public static class ProductionFragment extends Fragment {   
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.something, container);
        return view;
    }
}

The solution was one of the following:

//The preferred way
View view = inflater.inflate(R.layout.something, container, false);

//Not optimal but still working way
View view = inflater.inflate(R.layout.something, null);

When I call inflater.inflate() method with two params, the inflater implicitly tries to attach the inflated view to the container. This causes the newly created view to be already attached to the fragment even if we still didn't return our view.

If we pass a null as the second param, Android Studio reminds us that

When inflating a layout, avoid passing in null as the parent view, since otherwise any layout parameters on the root of the inflated layout will be ignored.

technically driving us to fill the second param.

The best way to manage it is to keep track of the container parameters by passing it to the function and NOT adding our view to the root. This is done by calling inflate with three parameters:

inflater.inflate(R.layout.something, container, false);

where the last false tells the inflate method to not add the view to the container view we've supplied.

So, to prevent this mistake in the future, how to programmatically decide what overload of inflate method use?

  • Are you implementing a method that asks you to return a view and the view appending is not not meant to be done by you? (Fragments, ListAdapter of ListView/Spinners...)

    Use inflater.inflate(R.layout.something, container, false);

  • Are you building your interface from code and what you're loading is going to be the container?

    Use inflater.inflate(R.layout.something, null);

  • Are you inflating something that you're planning to manually add to an another container?

    Use inflater.inflate(R.layout.something, container); and do not add the item to the container (it already does it).

like image 134
Bob Avatar answered Sep 30 '22 04:09

Bob