2017-04-02 23 views
0

嗨我相當新的android工作室,我正在爲我的學校項目建立一個應用程序。 我有一個ImageButtons生成的數組列表,我需要將它們分配到TableView中的行中,使它看起來像一個元素週期表。不過,我不斷收到此錯誤:指定的孩子已經有一位家長。我在想什麼

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)  

這是代碼的活動,這是在發生的事情:

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

如果有任何其他信息,將是有益的,讓我知道了,謝謝!

+2

你的代碼是一種混亂,但在tableGen()的內部'for'循環中檢查循環計數器和數組索引的邏輯。第一行是添加'Button's 0-17,然後第二行試圖再次從17開始添加'Button' - tempRow.addView(imageButtons [temp-1]);'。 –

+0

謝謝!我tottaly錯過了,應該更加小心。我曾試過一些東西,因此爲什麼代碼如此混亂和被忽視。現在工作。 – Dan

+0

如果您對每種情況都有完全不同的邏輯,那麼您不應該有循環;它打敗了循環的目的。如果'createButtons'應該是一個類型,它應該拼寫成'CreateButtons'。 –

回答

1

你的問題可能是因爲以下行:

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

您反覆創建它。你甚至沒有使用它。嘗試從循環中刪除上面的行並檢查。如果您仍然遇到任何問題,請告訴我。

相關問題