我正在尝试添加一些对象,TableRow
但出现以下错误:
05-11 13:43:34.494: E/AndroidRuntime(3258): FATAL EXCEPTION: main
05-11 13:43:34.494: E/AndroidRuntime(3258): java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
05-11 13:43:34.494: E/AndroidRuntime(3258): at android.view.ViewGroup.addViewInner(ViewGroup.java:3618)
05-11 13:43:34.494: E/AndroidRuntime(3258): at android.view.ViewGroup.addView(ViewGroup.java:3489)
05-11 13:43:34.494: E/AndroidRuntime(3258): at android.widget.TableLayout.addView(TableLayout.java:425)
05-11 13:43:34.494: E/AndroidRuntime(3258): at android.view.ViewGroup.addView(ViewGroup.java:3434)
这是我的代码:
public class LruView extends TableLayout
{
private Context c;
public LruView(Context context, AttributeSet attrs)
{
super(context, attrs);
this.c = context;
}
public LruView(Context context)
{
super(context);
this.c = context;
}
public void setData(BaseAdapter mAdapter)
{
int columnCounter = 0;
int totalColumns = getNumColumns();
for (int i = 0; i < mAdapter.getCount(); i++)
{
if (i < totalColumns)
{
TableRow mTableRow = new TableRow(c);
for (int j = 0; j < totalColumns; j++)
{
View mView = mAdapter.getView(columnCounter, null, null);
mTableRow.addView(mView);
this.addView(mTableRow);
columnCounter++;
}
}
}
}
public int getNumColumns()
{
float scalefactor = getResources().getDisplayMetrics().density * 100;
int number = ((Activity) c).getWindowManager().getDefaultDisplay().getWidth();
int columns = (int) ((float) number / (float) scalefactor);
int deviceOrientation = getResources().getConfiguration().orientation;
if (Configuration.ORIENTATION_LANDSCAPE == deviceOrientation)
return ((columns - 3) <= 2) ? 3 : (columns - 3);
else
return ((columns - 1) <= 2) ? 2 : (columns - 1);
}
}
关于如何做到这一点的任何想法?