Java.lang.StackOverflowError : stack size 8MB in Views error

Java.lang.StackOverflowError : stack size 8MB in Views error … here is a solution to the problem.

Java.lang.StackOverflowError : stack size 8MB in Views error

This code causes me a java.lang.StackOverflowError: stack size 8MB error, any idea why? I’m trying to use TableLayout and TableRow in NestedScrollView.

String testString = "test";
tableLayout = (TableLayout) findViewById(R.id.tableLayout1);
TextView textInRow = new TextView(this)
TableRow tableRow = new TableRow(this);
textInRow.setText(testString);
tableRow.addView(tableRow);
tableLayout.addView(tableRow);

Here is my Activity xml file:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context="eu.martinbednar.mayak.TripList"
    tools:showIn="@layout/activity_scrolling"
    android:id="@+id/table">

<TableLayout
        android:id="@+id/tableLayout1"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:padding="10dip"
        android:isScrollContainer="true">
    <TableRow
        android:id="@+id/tableRow1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
    </TableRow>
    </TableLayout>

</android.support.v4.widget.NestedScrollView>

Thank you for your help.

Solution

It’s hard to say, but I guess so

tableRow.addView(tableRow);

It could be the reason. Adding a View to itself can be the cause of infinite recursion, hence the StackOverflowError

Related Problems and Solutions