java.lang.StackOverflowError:视图中的堆栈大小为 8MB

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/42609182/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 06:45:47  来源:igfitidea点击:

java.lang.StackOverflowError: stack size 8MB in Views

javaandroid

提问by Blackbelt

This piece of code causes me java.lang.StackOverflowError: stack size 8MBerror, any idea why? I am trying to have TableLayout and TableRow inside NestedScrollView.

这段代码导致我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);

here is my activity xml file:

这是我的活动 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>

Thank you for help.

谢谢你的帮助。

回答by Blackbelt

Hard to say, but I guess this

很难说,但我想这个

tableRow.addView(tableRow);

may be the cause. Adding the view to itself it is probably cause of a infinite recursion, hence the StackOverflowError

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

回答by RaghavPai

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

Use this instead.

改用这个。