StackOverFlowError只有两个嵌套布局

时间:2016-03-20 20:26:39

标签: android stack-overflow android-tablelayout

我收到了StackOverFlowError。我有动态添加按钮的相对布局和表格布局。谁能帮我这个?这是我的代码:

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    populateButtons();
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}

private void populateButtons() {
    // TODO Auto-generated method stub
    TableLayout table=(TableLayout) findViewById(R.id.tableForButtons);
    for(int row=0; row<2; row++)
    {
        TableRow tableRow=new TableRow(this);
        tableRow.addView(tableRow);

        for(int col=0; col<3; col++)
        {
            Button button=new Button(this);
            tableRow.addView(button);
        }

    }

}

}

这是我的XML:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.testtablea.MainActivity" >

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/hello_world" />

<TableLayout
    android:id="@+id/tableForButtons"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/textView1"
    android:layout_below="@+id/textView1"
    android:layout_marginLeft="26dp"
    android:layout_marginTop="45dp" >

    <TableRow
        android:id="@+id/tableRow1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >
    </TableRow>

    <TableRow
        android:id="@+id/tableRow2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >
    </TableRow>

    <TableRow
        android:id="@+id/tableRow3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >
    </TableRow>

    <TableRow
        android:id="@+id/tableRow4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >
        </TableRow>
    </TableLayout>

</RelativeLayout>

好吧,我发现这个错误是因为许多嵌套布局。但我只有两个。 :(

logcat的:

03-20 21:40:02.119: E/AndroidRuntime(9139): FATAL EXCEPTION: main
03-20 21:40:02.119: E/AndroidRuntime(9139): Process: com.example.testtablea, PID: 9139
03-20 21:40:02.119: E/AndroidRuntime(9139): java.lang.StackOverflowError
03-20 21:40:02.119: E/AndroidRuntime(9139):     at android.view.ViewGroup.resetResolvedLayoutDirection(ViewGroup.java:5722)
03-20 21:40:02.119: E/AndroidRuntime(9139):     at android.view.ViewGroup.resetResolvedLayoutDirection(ViewGroup.java:5728)
03-20 21:40:02.119: E/AndroidRuntime(9139):     at android.view.ViewGroup.resetResolvedLayoutDirection(ViewGroup.java:5728)
03-20 21:40:02.119: E/AndroidRuntime(9139):     at android.view.ViewGroup.resetResolvedLayoutDirection(ViewGroup.java:5728)
03-20 21:40:02.119: E/AndroidRuntime(9139):     at android.view.ViewGroup.resetResolvedLayoutDirection(ViewGroup.java:5728)
03-20 21:40:02.119: E/AndroidRuntime(9139):     at android.view.ViewGroup.resetResolvedLayoutDirection(ViewGroup.java:5728)
03-20 21:40:02.119: E/AndroidRuntime(9139):     at android.view.ViewGroup.resetResolvedLayoutDirection(ViewGroup.java:5728)
03-20 21:40:02.119: E/AndroidRuntime(9139):     at android.view.ViewGroup.resetResolvedLayoutDirection(ViewGroup.java:5728)
03-20 21:40:02.119: E/AndroidRuntime(9139):     at android.view.ViewGroup.resetResolvedLayoutDirection(ViewGroup.java:5728)
03-20 21:40:02.119: E/AndroidRuntime(9139):     at android.view.ViewGroup.resetResolvedLayoutDirection(ViewGroup.java:5728)
03-20 21:40:02.119: E/AndroidRuntime(9139):     at android.view.ViewGroup.resetResolvedLayoutDirection(ViewGroup.java:5728)
03-20 21:40:02.119: E/AndroidRuntime(9139):     at android.view.ViewGroup.resetResolvedLayoutDirection(ViewGroup.java:5728)
03-20 21:40:02.119: E/AndroidRuntime(9139):     at android.view.ViewGroup.resetResolvedLayoutDirection(ViewGroup.java:5728)
03-20 21:40:02.119: E/AndroidRuntime(9139):     at android.view.ViewGroup.resetResolvedLayoutDirection(ViewGroup.java:5728)
03-20 21:40:02.119: E/AndroidRuntime(9139):     at android.view.ViewGroup.resetResolvedLayoutDirection(ViewGroup.java:5728)
03-20 21:40:02.119: E/AndroidRuntime(9139):     at android.view.ViewGroup.resetResolvedLayoutDirection(ViewGroup.java:5728)
03-20 21:40:02.119: E/AndroidRuntime(9139):     at android.view.ViewGroup.resetResolvedLayoutDirection(ViewGroup.java:5728)
03-20 21:40:02.119: E/AndroidRuntime(9139):     at android.view.ViewGroup.resetResolvedLayoutDirection(ViewGroup.java:5728)
03-20 21:40:02.119: E/AndroidRuntime(9139):     at android.view.ViewGroup.resetResolvedLayoutDirection(ViewGroup.java:5728)
03-20 21:40:02.119: E/AndroidRuntime(9139):     at 
...
android.view.ViewGroup.resetResolv

1 个答案:

答案 0 :(得分:3)

我还没有运行你的代码,但是这行可能会让你的代码出错: out tableRow.addView(tableRow);正在尝试添加自己。