嗨,我是android studio的新手,我正在为我的学校项目构建应用程序。 我有一个生成的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);
}
}
}
如果有任何其他信息有用,请告诉我,谢谢!
答案 0 :(得分:1)
您的问题可能是因为以下几行:
TableRow rowOne = new TableRow(this);
rowOne.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT,
TableRow.LayoutParams.WRAP_CONTENT));
您正在反复创建它。你甚至没有使用它。尝试从循环中删除上面的行并检查。如果您仍然遇到任何问题,请告诉我。