0

This is my piece of code to show ListPopupWindow

listPopupWindow = new ListPopupWindow(this); 
        listPopupWindow.setAnchorView(this.findViewById(android.R.id.content)); 

        final String[]messages = new String[MsgFromApp.getMessages().length];

        final ListAdapter adapter = new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line, messages); 
        final AdapterView.OnItemClickListener itemClickListener = new AdapterView.OnItemClickListener() { 
            @Override 
            public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) { 
                listPopupWindow.dismiss(); 
                listPopupWindow = null;
            } 
        };
        listPopupWindow.setModal(true);     listPopupWindow.setListSelector(getResources().getDrawable(R.drawable.message)); 
        listPopupWindow.setAdapter(adapter); 
        listPopupWindow.setOnItemClickListener(itemClickListener); 
        listPopupWindow.show();

It gives following exception: Caused by: android.view.WindowManager$BadTokenException: Unable to add window -- token null is not valid; is your activity running?

What's the problem with this? & how to fix this?

Selena
  • 41
  • 5
  • The problem is in line `listPopupWindow = new ListPopupWindow(this); ` What does "this" refers to? Is in an Activity? – geNia Jan 20 '16 at 22:35

1 Answers1

0

This exception occurs when the app is trying to notify the user from the background thread (AsyncTask) by opening a Dialog. Check this link: https://stackoverflow.com/a/18665887/4848308

Community
  • 1
  • 1
Gueorgui Obregon
  • 4,688
  • 2
  • 29
  • 54