hackingforgirls February 2016

Show ListPopupWindow from PopWindow

I have a PopupWindow with a button. On I click on the button I want to show a ListPopupWindow but I get

FATAL EXCEPTION: main
android.view.WindowManager$BadTokenException: Unable to add window -- token android.view.ViewRootImpl$W@439c79f0 is not valid; is your activity running?

and this is my show() method called onClick(). I get the error on the last line

popup.show();

full:

void showList(View view){

    final ListPopupWindow popup = new ListPopupWindow(this);
    popup.setAdapter(new ArrayAdapter<String>(this,
            android.R.layout.simple_list_item_1, Category));
    popup.setAnchorView(view);
    popup.setWidth(ListPopupWindow.WRAP_CONTENT);
    popup.setOnItemClickListener(new AdapterView.OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view,
                                int position, long id) {
            tvCategory.setText(Category[position]);
            popup.dismiss();
        }
    });

    popup.show();


}

FulllActivity code:

public class MapSights extends Activity implements OnMapReadyCallback {

ImageView btnPlus, close_add_sight;
PopupWindow pw, add_sg;
PopupMenu popupMenuCategory;
ListPopupWindow listPopupWindow;
int ONE = 1;
TextView tvCategory;
LinearLayout add_sight_btn;
Context context;
String TAG = "MapSights.java";

private static final String[] Category = { "Amphibian",
        "Fish", "Reptile", "Bird","Mammal",
        "Miriapode", "Insect", "Arachnid","Shellfish",
        "Snail", "Shell", "Slime mold","Fungi",
        "Plant", "Anemone", "Coral"};


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.map_sights);

    Log.d(TAG, "onCreate");


    context = getApplicationContext();

    MapFragment mapFragment = (MapFragment) getFragmentManager().findFragmentById(R.        

Answers


g2o February 2016

Change final ListPopupWindow popup = new ListPopupWindow(this); to final ListPopupWindow popup = new ListPopupWindow(MapSights.this);

Try to check if the activity is finishing before call popup.show(); to avoid Error is your activity running?

if(!isFinishing()) {
    popup.show();
}

Post Status

Asked in February 2016
Viewed 3,476 times
Voted 7
Answered 1 times

Search




Leave an answer