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?