【问题标题】:java.lang.StackOverflowError: stack size 8MB in Viewsjava.lang.StackOverflowError:视图中的堆栈大小为 8MB
【发布时间】:2017-03-05 22:45:07
【问题描述】:

这段代码导致我java.lang.StackOverflowError: stack size 8MB 错误,知道为什么吗?我试图在 NestedScrollView 中有 TableLayout 和 TableRow。

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

这是我的活动 xml 文件:

<?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>

感谢您的帮助。

【问题讨论】:

    标签: java android


    【解决方案1】:

    很难说,但我猜是这个

    tableRow.addView(tableRow);
    

    可能是原因。将视图添加到自身可能是无限递归的原因,因此StackOverflowError

    【讨论】:

    • 递归是我的原因
    【解决方案2】:
        String testString = "test";
        tableLayout = (TableLayout) findViewById(R.id.tableLayout1);
        TextView textInRow = new TextView(this)
        TableRow tableRow = new TableRow(this);
        textInRow.setText(testString);
        tableRow.addView(textInRow);
        tableLayout.addView(tableRow);
    

    改用这个。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-04-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-08-04
      • 2016-09-01
      相关资源
      最近更新 更多