2017-04-02 5 views
0

こんにちは、私はアンドロイドスタジオにはかなり新しいです。私は自分の学校プロジェクトのためのアプリを作ります。 私は、生成されたImageButtonの配列リストを持っています。私はそれを周期テーブルのように見せるために、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を追加し、次に2番目の行は' Button'sを17から再び追加しようとしています - '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)); 

。あなたはそれを使っていません。ループから上記の行を削除して確認してください。あなたはまだ何か問題に直面している場合は教えてください。

関連する問題