2017-04-02 3 views
0

Hallo, ich bin ziemlich neu in Android Studio und ich baue eine App für mein Schulprojekt. Ich habe eine Array-Liste von ImageButtons, die generiert wird, und ich muss sie in Zeilen in meiner TableView zuweisen, damit es wie eine Periodentabelle aussieht. Allerdings halten Empfang ich diesen Fehler:Das angegebene Kind hat bereits einen Elternteil. Was vermisse ich

package ros_dhhiggins.example.com.periodictable; 
import android.content.Context; 
import android.os.Bundle; 
import android.support.v7.app.AppCompatActivity; 
import android.view.ViewGroup; 
import android.widget.ImageButton; 
import android.widget.TableLayout; 
import android.widget.TableRow; 
public class PeriodicTableScreen extends AppCompatActivity { 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_periodic_table_screen); 
    TableLayout table = (TableLayout) findViewById(R.id.LayoutTable); 
    createButtons newButtons = new createButtons(this); 
    tableGen(table, newButtons); 
} 

public void tableGen(TableLayout table, createButtons newButtons) { 
    ImageButton[] imageButtons; 
    imageButtons = newButtons.build(); 
    for(int j = 1; j <= 7; j++){ 
      TableRow tempRow = new TableRow(this); 
     if(j==1) { 
      tempRow.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, 
        TableRow.LayoutParams.WRAP_CONTENT)); 

      for (int temp = 0; temp <= 17; temp++) { 
       ImageButton tempButton = imageButtons[temp]; 
       tempButton.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, 
         TableRow.LayoutParams.WRAP_CONTENT)); 
       tempRow.addView(imageButtons[temp]); 
      } 
     } 
     else if (j==2){ 
       for(int temp = 18; temp <=35; temp++){ 
        TableRow rowOne = new TableRow(this); 
        rowOne.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, 
          TableRow.LayoutParams.WRAP_CONTENT)); 
        ImageButton tempButton = imageButtons[temp]; 
        tempButton.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, 
          TableRow.LayoutParams.WRAP_CONTENT)); 
        tempRow.addView(imageButtons[temp-1]); 
       } 
     } 
     else if (j==3){ 
      TableRow rowOne = new TableRow(this); 
      rowOne.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, 
        TableRow.LayoutParams.WRAP_CONTENT)); 
       for(int temp = 36; temp <=53; temp++){ 
        ImageButton tempButton = imageButtons[temp]; 
        tempButton.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, 
          TableRow.LayoutParams.WRAP_CONTENT)); 
        tempRow.addView(imageButtons[temp-1]); 
       } 
     } 
     else if (j==4){ 
      TableRow rowOne = new TableRow(this); 
      rowOne.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, 
        TableRow.LayoutParams.WRAP_CONTENT)); 
       for(int temp = 54; temp <=71; temp++){ 
        ImageButton tempButton = imageButtons[temp]; 
        tempButton.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, 
          TableRow.LayoutParams.WRAP_CONTENT)); 
        tempRow.addView(imageButtons[temp-1]); 
       } 
     } 
     else if (j==5){ 
      TableRow rowOne = new TableRow(this); 
      rowOne.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, 
        TableRow.LayoutParams.WRAP_CONTENT)); 
       for(int temp = 72; temp <=89; temp++){ 
        ImageButton tempButton = imageButtons[temp]; 
        tempButton.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, 
          TableRow.LayoutParams.WRAP_CONTENT)); 
        tempRow.addView(imageButtons[temp-1]); 
       } 
     } 
     else if (j==6){ 

       for(int temp = 90; temp <=107; temp++){ 
        ImageButton tempButton = imageButtons[temp]; 
        tempButton.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, 
          TableRow.LayoutParams.WRAP_CONTENT)); 
        tempRow.addView(imageButtons[temp-1]); 
       } 
     } 
     else if (j==7){ 
       for(int temp = 108; temp <=125; temp++){ 
        ImageButton tempButton = imageButtons[temp]; 
        tempButton.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, 
          TableRow.LayoutParams.WRAP_CONTENT)); 
        tempRow.addView(imageButtons[temp-1]); 
       } 
     } 
     table.addView(tempRow, new TableLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT, 
       TableLayout.LayoutParams.WRAP_CONTENT)); 
     ((ViewGroup) tempRow.getParent()).removeView(tempRow); 
    } 
    } 
} 

Wenn es irgendeine andere Informationen, die nützlich sein würde lassen Sie mich wissen, danke:

java.lang.RuntimeException: Unable to start activity ComponentInfo{ros_dhhiggins.example.com.periodictable/ros_dhhiggins.example.com.periodictable.PeriodicTableScreen}: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first. 
                         at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2325) 
                         at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387) 
                         at android.app.ActivityThread.access$800(ActivityThread.java:151) 
                         at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303) 
                         at android.os.Handler.dispatchMessage(Handler.java:102) 
                         at android.os.Looper.loop(Looper.java:135) 
                         at android.app.ActivityThread.main(ActivityThread.java:5254) 
                         at java.lang.reflect.Method.invoke(Native Method) 
                         at java.lang.reflect.Method.invoke(Method.java:372) 
                         at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903) 
                         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698) 
                        Caused by: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first. 
                         at android.view.ViewGroup.addViewInner(ViewGroup.java:3936) 
                         at android.view.ViewGroup.addView(ViewGroup.java:3786) 
                         at android.view.ViewGroup.addView(ViewGroup.java:3727) 
                         at android.view.ViewGroup.addView(ViewGroup.java:3700) 
                         at ros_dhhiggins.example.com.periodictable.PeriodicTableScreen.tableGen(PeriodicTableScreen.java:46) 
                         at ros_dhhiggins.example.com.periodictable.PeriodicTableScreen.onCreate(PeriodicTableScreen.java:18) 
                         at android.app.Activity.performCreate(Activity.java:5990) 
                         at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106) 
                         at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278) 
                         at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)  
                         at android.app.ActivityThread.access$800(ActivityThread.java:151)  
                         at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)  
                         at android.os.Handler.dispatchMessage(Handler.java:102)  
                         at android.os.Looper.loop(Looper.java:135)  
                         at android.app.ActivityThread.main(ActivityThread.java:5254)  
                         at java.lang.reflect.Method.invoke(Native Method)  
                         at java.lang.reflect.Method.invoke(Method.java:372)  
                         at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)  
                         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)  

Dies ist der Code für die Aktivität dieser in geschieht!

+2

Ihr Code ist eine Art Durcheinander, aber überprüfen Sie Ihre Logik auf die Schleifenzähler und Array-Indizes in den inneren 'for' Schleifen in' tableGen() '. Die erste Zeile fügt 'Button's 0-17 hinzu, dann versucht die zweite Zeile, die' 'Button's von 17 wieder hinzuzufügen -' tempRow.addView (imageButtons [temp-1]); '. –

+0

Danke! Das habe ich leider vermisst, hätte vorsichtiger sein müssen. Ich hatte etwas ausprobiert, weshalb der Code so unordentlich war und das übersehen hatte. Funktioniert jetzt. – Dan

+0

Sie sollten keine Schleife haben, wenn Sie für jeden Fall eine völlig andere Logik haben; es vereitelt den Zweck einer Schleife. Und wenn 'createButtons' ein Typ sein soll, sollte es als' CreateButtons' geschrieben werden. –

Antwort

1

Ihr Problem könnte wegen folgenden Linien:

TableRow rowOne = new TableRow(this); 
      rowOne.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, 
        TableRow.LayoutParams.WRAP_CONTENT)); 

Du es immer wieder zu schaffen. Du benutzt es nicht einmal. Versuchen Sie, die obigen Zeilen aus Ihrer Schleife zu entfernen und überprüfen Sie sie. Lassen Sie mich wissen, wenn Sie noch ein Problem haben.

Verwandte Themen