Android PopupWindow You must call removeView() on the child's parent first.

来源:互联网 发布:mysql安装教程5.7.17 编辑:程序博客网 时间:2024/05/21 21:43
package com.mcdull.picker.ui;import android.app.Activity;import android.content.Intent;import android.graphics.drawable.ColorDrawable;import android.view.Gravity;import android.view.LayoutInflater;import android.view.View;import android.widget.AdapterView;import android.widget.ListView;import android.widget.PopupWindow;import com.hanshuang.mcdull.picker.R;import com.hanshuang.mcdull.picker.adapter.PhotoAlbumLVAdapter;import com.hanshuang.mcdull.picker.model.PhotoModel;import java.util.ArrayList;/** *选择相册 */public class PhotoAlbumPopwindow extends PopupWindow implements AdapterView.OnItemClickListener{    private LayoutInflater mLayoutInflater;    private Activity mContext;    private ListView mListView;    private ArrayList<PhotoModel> list;    public PhotoAlbumPopwindow(Activity context) {        super();        mLayoutInflater = LayoutInflater.from(context);        this.mContext = context;        initView();    }    private void initView() {        View view = mLayoutInflater.inflate(R.layout.popwindow_layout,null);        mListView = (ListView) view.findViewById(R.id.popwindow_layout_list);        int h = mContext.getWindowManager().getDefaultDisplay().getHeight();        int w = mContext.getWindowManager().getDefaultDisplay().getWidth();        // 设置SelectPicPopupWindow的View        this.setContentView(mListView);        // 设置SelectPicPopupWindow弹出窗体的宽        this.setWidth(w);        // 设置SelectPicPopupWindow弹出窗体的高        this.setHeight(h);        // 设置SelectPicPopupWindow弹出窗体可点击        this.setFocusable(true);        this.setOutsideTouchable(true);        // 刷新状态        this.update();        // 实例化一个ColorDrawable颜色为半透明        ColorDrawable dw = new ColorDrawable(0xb0000000);        // 点back键和其他地方使其消失,设置了这个才能触发OnDismisslistener ,设置其他控件变化等操作        this.setBackgroundDrawable(dw);        mListView.setOnItemClickListener(this);    }    /**     * 显示popupWindow     *     * @param parent     */    public void showPopupWindow(View parent, ArrayList<PhotoModel> list) {        if(!list.isEmpty()) this.list=list;        if (!this.isShowing()) {            // 以下拉方式显示popupwindow            PhotoAlbumLVAdapter adapter = new PhotoAlbumLVAdapter(mContext, list);            mListView.setAdapter(adapter);            this.showAsDropDown(parent, Gravity.BOTTOM, 0);        } else {            this.dismiss();        }    }    @Override    public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {        Intent intent = new Intent(mContext, PhotoWallActivity.class);        intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);        //第一行为“最近照片”        if (position == 0) {            intent.putExtra("code", 200);        } else {            intent.putExtra("code", 100);            intent.putExtra("folderPath", list.get(position).getPathName());        }        mContext.startActivity(intent);        this.dismiss();    }}
运行起来报错:<span style="color:#ff0000;">You must call removeView() on the child's parent first</span>. 


后来发现问题原来在给PopupWindow 设置View上

因为ListView属于View 所以不能直接set子布局

 this.setContentView(mListView);

this.setContentView(view);

解决,代码有的时候写的太马虎,以后要多注意.





0 0
原创粉丝点击