Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android StackOverflowError in ViewGroup.resetResolvedTextDirection when adding ListView to LinearLayout

I had asked one question about this error before (Android StackOverflowError in ViewGroup.resetResolvedTextDirection), however now I managed to reproduce the error in the emulator and narrowed down the specific place where this problem occurs.

When I'm starting my activity, I have an AsyncTask, which pulls the necessary data from my server and creates all views. Inside the run() method of the AsyncTask, I'm creating a custom view (and adding it to the main view of the activity - but that's not important now):

ListingView listing = new ListingView(MainActivity.this);

ListingView is my custom view class, which extends from LinearLayout. In the constructor of this custom view, I have this code:

public ListingView(Context context)
{
    this.context = context;
    ...

    LayoutInflater infl = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View top = infl.inflate(R.layout.listing, this, true);
    this.addView(top);
    ...

    LinearLayout lst = (LinearLayout)this.findViewById(R.id.LayoutListingTop);
    tblVenues = new ListView(context);
    tblVenues.setVisibility(VISIBLE);
    tblVenues.setItemsCanFocus(true);
    tblVenues.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            if(venueListener != null && venues.size() > 0) { venueListener.selected(venues.get(position)); }
        }
    });
    LayoutParams lp = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
    lst.addView(tblVenues, lp);    //this line causes the problem

    ...
}

In this custom view class, tblVenues is declared as

private ListView tblVenues;

And the XML that's being loaded is this:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:id="@+id/LayoutListingTop"
              android:layout_width="fill_parent"
              android:layout_height="fill_parent"
              android:background="@color/white">
    <ImageView android:id="@+id/ImageView01"
               android:layout_width="170dp"
               android:layout_height="62dp"
               android:src="@drawable/ookl_logo"
               android:layout_gravity="left"
               android:adjustViewBounds="false"
               android:scaleType="fitXY">
    </ImageView>

    <TextView android:layout_height="wrap_content"
              android:layout_width="fill_parent"
              android:layout_marginBottom="5px"
              android:gravity="center"
              android:textColor="@color/black"
              android:text="@string/love_culture"
              android:id="@+id/TextView01"
              android:textSize="28dip">
    </TextView>

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
                  android:orientation="horizontal"
                  android:id="@+id/ButtonBar"
                  android:layout_width="fill_parent"
                  android:layout_height="wrap_content"
                  android:background="@drawable/buttonbar"
                  android:paddingLeft="10px"
                  android:paddingRight="10px"
                  android:paddingTop="5px"
                  android:paddingBottom="5px">

        <Button android:id="@+id/BtnListVenues"
                     android:layout_width="wrap_content"
                     android:layout_height="45dip"
                     android:background="@null"
                     android:text="@string/button_venues"
                     android:drawableTop="@drawable/icon_venues_on"
                     android:textColor="@color/venue_blue"
                     android:layout_marginRight="10px"
                     android:textSize="9dip">
        </Button>

        <Button android:id="@+id/BtnListEvents"
                     android:layout_width="wrap_content"
                     android:layout_height="45dip"
                     android:background="@null"
                     android:text="@string/button_events"
                     android:drawableTop="@drawable/icon_events_off"
                     android:textColor="@color/white"
                     android:layout_marginRight="10px"
                     android:textSize="9dip">
        </Button>

        <Button android:id="@+id/BtnListTrails"
                     android:layout_width="wrap_content"
                     android:layout_height="45dip"
                     android:background="@null"
                     android:text="@string/button_trails"
                     android:drawableTop="@drawable/icon_trails_off"
                     android:textColor="@color/white"
                     android:layout_marginRight="10px"
                     android:textSize="9dip">
        </Button>

        <Button android:id="@+id/BtnListObjects"
                     android:layout_width="wrap_content"
                     android:layout_height="45dip"
                     android:background="@null"
                     android:text="@string/button_objects"
                     android:drawableTop="@drawable/icon_objects_off"
                     android:textColor="@color/white"
                     android:layout_marginRight="10px"
                     android:textSize="9dip">
        </Button>

        <TextView android:id="@+id/TxtLabelDistance"
                  android:layout_width="fill_parent"
                  android:text="@string/label_distance"
                  android:layout_height="wrap_content"
                  android:gravity="right|bottom"
                  android:layout_alignParentBottom="true"
                  android:textColor="@color/white"
                  android:layout_marginTop="-17px"
                  android:textSize="9dip">
        </TextView>
    </LinearLayout>


</LinearLayout>

This all works perfectly fine on Android form 1.6 up to 3.x, however on Ice Cream Sandwich (4.0) line

lst.addView(tblVenues, lp);

results in StackOverflowError:

java.lang.StackOverflowError
at android.view.ViewGroup.resetResolvedTextDirection(ViewGroup.java:5131)
at android.view.ViewGroup.resetResolvedTextDirection(ViewGroup.java:5131)
at android.view.ViewGroup.resetResolvedTextDirection(ViewGroup.java:5131)
at android.view.ViewGroup.resetResolvedTextDirection(ViewGroup.java:5131)
at android.view.ViewGroup.resetResolvedTextDirection(ViewGroup.java:5131)
at android.view.ViewGroup.resetResolvedTextDirection(ViewGroup.java:5131)
... this line repeats about 200 times or so ...

There's nothing else useful in the LogCat. The whole ListingView is not even added to the main activity view yet, because the error is thrown essentially from its constructor.

I'm a total loss as to what may be the problem. Any help will be greatly appreciated.

like image 605
Aleks G Avatar asked Dec 06 '25 02:12

Aleks G


1 Answers

[Old question, but I wasn't pleased with the answer, so adding my notes in hope that they're helpful for someone else.]

Aleks -- I agree with the core issue being an Android bug, but tracing into the code (I encountered a similar issue recently) I believe the issue can be prevented by inflating with the attachToRoot parameter set to false. This is because with attacheToRoot==true, the returned view is the 'root' parameter you passed in. In your case that is 'this', which then add a child of the current view (you're adding the object as a child of itself). This seems to confuse android (it really should check for instance equality).

http://developer.android.com/reference/android/view/LayoutInflater.html#inflate(org.xmlpull.v1.XmlPullParser, android.view.ViewGroup, boolean)

"[Returns] The root View of the inflated hierarchy. If root was supplied and attachToRoot is true, this is root; otherwise it is the root of the inflated XML file."

Making this change resolved the issue in my case.

like image 167
DSK Avatar answered Dec 08 '25 10:12

DSK



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!