2016-03-20 8 views
0

Ich bekomme StackOverFlowError. Ich habe relative Layout und Tabellenlayout für dynamische Hinzufügen von Schaltflächen. Kann mir jemand dabei helfen? Hier ist mein Code:StackOverFlowError mit nur zwei verschachtelten Layouts

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

    } 

} 

}

Und hier ist meine 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> 

Nun, fand ich, dass dieser Fehler, da viele verschachtelte Layouts ist. Aber ich habe nur zwei. :(

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 
+0

ist, dass Ihre komplette XML-Datei –

+1

@ZaidQureshi, ja ... Ich habe dieses Tutorial gefunden: https://www.youtube.com/watch?v=4MFzuP1F-xQ – Darman

+0

Sie verpassen das Ende Ihrer relativen Layout –

Antwort

2

Ich habe den Code nicht ausgeführt, aber vielleicht diese Zeile Code immer Fehler machen. tableRow.addView(tableRow); tableRow versucht, sich fügen

+0

Oh Gott ... Ich habe 2 Stunden mit dem Debuggen meiner Haupt-App verloren. Ich danke dir sehr!!!! :) – Darman