user4689328
user4689328

Reputation: 5

An exception display when I build a PopupWindow?

I want to build a popupWindow(there is a button), when I click the button, I need to build a popupWindow to left of above popupWindow, thus a exception displays (android.view.WindowManager$BadTokenException: Unable to add window -- token android... Is activity running?). How can I do?

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);  
    }
}

Upvotes: 0

Views: 67

Answers (1)

Don Chakkappan
Don Chakkappan

Reputation: 7560

The problem is with your context. Instead getApplicationContext() use YourActivity.this

Upvotes: 1

Related Questions