Mindaugas Svirskas
Mindaugas Svirskas

Reputation: 1049

android java.lang.StackOverflowError in listview

I am getting a strange error android java.lang.StackOverflowError while loading entries in my listview. It is strange in a way that it always happens the 3rd time I am loading my listview.

The idea behind is that I have a map with a lot of markers (shops). User can click menu -> show shop list and he gets the populated list. Then he can press on the particular shop in the list and he is getting the same map back, but the shop is focused then. Then he can do the same actions again: menu -> show shop list.

In fact there is some kind of a loop, but the user decides himself whether he wants to go rounds (map -> list -> map...) or he can (by pressing on the pin) simply go out...

Everything works fine for the first 2 times...The third time I want to load the list, app crashes.

Log:

02-15 13:10:00.632: E/AndroidRuntime(23826): FATAL EXCEPTION: main
02-15 13:10:00.632: E/AndroidRuntime(23826): java.lang.StackOverflowError
02-15 13:10:00.632: E/AndroidRuntime(23826):    at android.view.View.draw(View.java:6880)
02-15 13:10:00.632: E/AndroidRuntime(23826):    at android.view.ViewGroup.drawChild(ViewGroup.java:1646)
02-15 13:10:00.632: E/AndroidRuntime(23826):    at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
02-15 13:10:00.632: E/AndroidRuntime(23826):    at android.view.ViewGroup.drawChild(ViewGroup.java:1644)
02-15 13:10:00.632: E/AndroidRuntime(23826):    at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
02-15 13:10:00.632: E/AndroidRuntime(23826):    at android.widget.AbsListView.dispatchDraw(AbsListView.java:1480)
02-15 13:10:00.632: E/AndroidRuntime(23826):    at android.widget.ListView.dispatchDraw(ListView.java:3138)
02-15 13:10:00.632: E/AndroidRuntime(23826):    at android.view.View.draw(View.java:6986)

I've read a lot of posts, that this error mostly appears when you have infinite loop. So is this some kind of android interpretation that when you go from one activity to another few times, it thinks you have infinite loop? I have no other ideas why could it happen. (One more thing to add, these 2 activities are under the same tab, I mean I use the same tab, but changing the content).

The code for changing content:

Tabs s = (Tabs) getParent();
        Intent myIntent = new Intent(getApplicationContext(), Map.class);
        myIntent.putExtra("playgroundID", playgroundsArrayHolder.ID);
        View view = s.getLocalActivityManager().startActivity("map",
            myIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)).getDecorView();
        setContentView(view);

EDIT:

I sort of got it to work with one cheat. I merged 2 activities to 1 (1 xml - hiding components I don't need at the moment). In this case I don't have to reload my activity each time user leaves it. I just show / hide components.

I know it is a big cheat, but thats the only way I got it to work. I've tried making attributes static and keep the adapter bound to it but it didn't work. And the problem actually appeared when I was setting adapter to listview.

Any further help is appreciated. Kind'a don't want to stay with 2 activities in 1 :/


OK, the problem still remains. Any help is appreciated

I tested the program with 2 simple activities (relative layouts hosting 1 button each) under 1 tab. Each of them had a link to each other and it crashes after the 3rd loop.

I go from one activity to another with the following code:

Tabs s = (Tabs) getParent();
    Intent myIntent = new Intent(getApplicationContext(), Map.class);
    View view = s.getLocalActivityManager().startActivity("map",
        myIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)).getDecorView();
    setContentView(view);

It doesn't matter what layouts I have in the activities, basically it still crashes every 3rd loop.

I have no other ideas except that android interprets this navigation (from 1 activity to another under the same tab) as an infinitive loop? Which basically users does, not a loop that is being programmed by me.

Upvotes: 0

Views: 6520

Answers (1)

Sergey Benner
Sergey Benner

Reputation: 4431

OK here's what I found (quoted from Mark Murphy (a Commons Guy) )

It means that stack space is exhausted.

Your UI probably has too many layers in its hierarchy. Run the hierarchyviewer program and examine the chart showing your ViewGroups and what they all contain. Try to eliminate some layers (e.g., replace nested LinearLayouts with a single RelativeLayout, get rid of activities-in-tabs and switch to views-in-tabs).

My very rough rule of thumb:

  • If your hierarchy has <10 layers, you should have no problem
  • If your hierarchy has 10-14 layers, I get nervous
  • If your hierarchy has >=15 layers, it's gonna blow up sometimes

Upvotes: 8

Related Questions