&引用;指定的子项已具有父项。必须对子项';调用removeView();“父母优先”;android中的例外
我正在从服务器检索信息,并试图以表格格式显示它,因此使用json对象 代码:&引用;指定的子项已具有父项。必须对子项';调用removeView();“父母优先”;android中的例外,android,parent-child,tablelayout,Android,Parent Child,Tablelayout,我正在从服务器检索信息,并试图以表格格式显示它,因此使用json对象 代码: JSONArray jsonArray = new JSONArray(jsonResult); TableLayout tv=(TableLayout) findViewById(R.id.table); tv.removeAllViewsInLayout(); int flag=1;
JSONArray jsonArray = new JSONArray(jsonResult);
TableLayout tv=(TableLayout) findViewById(R.id.table);
tv.removeAllViewsInLayout();
int flag=1;
for (int i = 0; i < jsonArray.length() ; i++) {
//JSONObject object1 = jsonArray.getJSONObject(i);
TableRow tr=new TableRow(viewtimetable.this);
tr.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
if(flag==1)
{
TextView col1=new TextView(viewtimetable.this);
col1.setText("Day");
col1.setTextColor(Color.BLUE);
col1.setTextSize(15);
tr.addView(col1);
TextView col2=new TextView(viewtimetable.this);
col2.setPadding(10, 0, 0, 0);
col2.setTextSize(15);
col2.setText("7:30-9:10AM");
col2.setTextColor(Color.BLUE);
tr.addView(col2);
TextView col3=new TextView(viewtimetable.this);
col3.setPadding(10, 0, 0, 0);
col3.setText("9:20-11:00AM");
col3.setTextColor(Color.BLUE);
col3.setTextSize(15);
tr.addView(col3);
TextView col4=new TextView(viewtimetable.this);
col4.setPadding(10, 0, 0, 0);
col4.setText("11:10-12:50PM");
col4.setTextColor(Color.BLUE);
col4.setTextSize(15);
tr.addView(col4);
TextView col5=new TextView(viewtimetable.this);
col5.setPadding(10, 0, 0, 0);
col5.setText("1:40-3:20PM");
col5.setTextColor(Color.BLUE);
col5.setTextSize(15);
tr.addView(col5);
TextView col6=new TextView(viewtimetable.this);
col6.setPadding(10, 0, 0, 0);
col6.setText("3:30-5:00PM");
col6.setTextColor(Color.BLUE);
col6.setTextSize(15);
tr.addView(col6);
tv.addView(tr);
final View vline = new View(viewtimetable.this);
vline.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, 2));
vline.setBackgroundColor(Color.BLUE);
tv.addView(vline);
flag=0;
}
else
{
try{
JSONObject json_data = jsonArray.getJSONObject(i);
//Log.i("log_tag","id: "+json_data.getInt("f1")+
// ", Username: "+json_data.getString("f2")+
// ", No: "+json_data.getInt("f3"));
//((ViewGroup)tr.getParent()).removeView(tr);
TextView b=new TextView(viewtimetable.this);
String stime=json_data.getString("day");
b.setText(stime);
b.setTextColor(Color.RED);
b.setTextSize(15);
tr.addView(b);
TextView b1=new TextView(viewtimetable.this);
b1.setPadding(10, 0, 0, 0);
b1.setTextSize(15);
if(json_data.getString("slot").equals("7:30-9:10AM")){
String stime1=json_data.getString("subject");
b1.setText(stime1);
}
b1.setTextColor(Color.WHITE);
tr.addView(b1);
TextView b2=new TextView(viewtimetable.this);
b2.setPadding(10, 0, 0, 0);
if(json_data.getString("slot").equals("9:20-11:00AM")){
String stime2=json_data.getString("subject");
b2.setText(stime2);
}
b2.setTextColor(Color.RED);
b2.setTextSize(15);
tr.addView(b2);
TextView b3=new TextView(viewtimetable.this);
b3.setPadding(10, 0, 0, 0);
if(json_data.getString("slot").equals("11:10-12:50PM")){
String stime3=json_data.getString("subject");
b3.setText(stime3);
}
b3.setTextColor(Color.RED);
b3.setTextSize(15);
tr.addView(b2);
TextView b4=new TextView(viewtimetable.this);
b4.setPadding(10, 0, 0, 0);
if(json_data.getString("slot").equals("1:40-3:20PM")){
String stime4=json_data.getString("subject");
b4.setText(stime4);
}
b4.setTextColor(Color.RED);
b4.setTextSize(15);
tr.addView(b2);
TextView b5=new TextView(viewtimetable.this);
b5.setPadding(10, 0, 0, 0);
if(json_data.getString("slot").equals("3:30-5:00PM")){
String stime5=json_data.getString("subject");
b5.setText(stime5);
}
b5.setTextColor(Color.RED);
b5.setTextSize(15);
tr.addView(b2);
tv.addView(tr);
}catch(JSONException e){
content.setText("jsonexception");
}
final View vline1 = new View(viewtimetable.this);
vline1.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, 1));
vline1.setBackgroundColor(Color.WHITE);
tv.addView(vline1);
}
但在我的情况下,在哪里,如何以及为什么使用这个 在文本视图b3、b4和b5的代码中,您正在尝试执行
tr.addView(b2)代码>因此b2总共增加了4次。这应该是b3、b4和b5
另一方面,尝试学习如何读取崩溃日志。异常应该准确地说明错误发生在哪一行代码上,因此可能是在第二次出现tr.addView(b2)时代码>谢谢你发现了我愚蠢的错误。也谢谢你的建议。
((ViewGroup)scrollChildLayout.getParent()).removeView(scrollChildLayout);