构建PopupWindow时会出现异常?

时间:2015-04-01 04:43:52

标签: android

我想构建一个popupWindow(有一个按钮),当我单击该按钮时,我需要在popupWindow上方左侧构建一个popupWindow,因此异常会显示(android.view.WindowManager$BadTokenException: Unable to add window -- token android... Is activity running?)。我能怎么做?

class MyActivity extends Activity{

    PopupWindow selectPop;
    TextView _tv_reset,_tv_confirm;
    ListView lv;
    private HashMap<String,String> data;
    private SearchProductSelectAdapter adapt;

    public void initSelectPop(){
        //here is the layout of the first popupwindow
        View v = LayoutInflater.from(context).inflate(R.layout.search_product_select_pop_layout,null);
        //here is the ListView in the view v
        lv = (ListView) v.findViewById(R.id.search_product_select_lv);
        //here is the itemclicklistener of the lv
        lv.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                initParamSetPop(position);
            }
        });
        //here set adapter to lv
        lv.setAdapter(adapt);
        //here build a instance of PopupWindow
        selectPop = new PopupWindow(v,getResources().getDisplayMetrics().widthPixels *  1/3,LayoutParams.MATCH_PARENT);
        //here display the first PopupWindow
        selectPop.showAsDropDown(_tv_shaixuan, 0, 0);
    }
    PopupWindow paramSetPop;
    EditText _et_start_price,_et_end_price;
    ListView _lv;
    TextView confirm;
    //here init the second PopupWindow
    public void initParamSetPop(int position){
            //here is the second layout of PopupWindow
            View v = LayoutInflater.from(context).inflate(R.layout.price_param_set_layout, null);
        //here build the instance of the second PopupWindow
        paramSetPop = new PopupWindow(v,getResources().getDisplayMetrics().widthPixels * 1/4,LayoutParams.MATCH_PARENT);
        //here display the second PopupWindow
        paramSetPop.showAsDropDown(_tv_reset);  
    }
}

1 个答案:

答案 0 :(得分:1)

问题在于你的背景。相反,getApplicationContext()使用YourActivity.this