0

我正在尝试将空页脚 ( LinearLayout) 添加到我ListView的页脚,并在之后用我自己的视图填充此页脚:

private View footerButton;
private LinearLayout footer;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.rlist);

    footer=new LinearLayout(this);
    footer.setLayoutParams(new ListView.LayoutParams(ListView.LayoutParams.MATCH_PARENT, ListView.LayoutParams.WRAP_CONTENT));

    footerButton=((LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.footer_button, null, false);

    ListView list=(ListView) findViewById(R.id.list);
    list.addFooterView(footerButton,null,false);

    final UsersListAdapter adapter = new UsersListAdapter(this);
    list.setAdapter(adapter);

    //...

    new Thread() {
        @Override
        public void run() {

            //Loading...

            runOnUiThread(new Runnable() {
                public void run() {
                    adapter.notifyDataSetChanged();

                    footer.removeAllViews();
                    footer.addView(footerButton);
                }
            });
        }
    }.start();
}

但我在“footer.addView(footerButton);”行中遇到异常:

java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.

为什么?如您所知,我会removeAllViews()在添加任何视图之前致电。如何解决?

4

1 回答 1

1

这是因为您在对象中添加了footerButtonListView

看看这条线..

list.addFooterView(footerButton,null,false);

所以,现在 footerButton 有一个父 ie 列表.. 所以你在添加到LinearLayout页脚时会出错

尝试removeView()列表...

于 2013-06-13T11:27:41.633 回答