从PopWindow中显示ListPopupWindow

时间:2016-02-08 15:03:34

标签: android popupwindow

我有一个带按钮的PopupWindow。在我点击我想要显示ListPopupWindow的按钮,但我得到

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

这是我的show()方法,名为onClick()。我在最后一行得到了错误

popup.show();

全:

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代码:

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.id.map);
    mapFragment.getMapAsync(this);

    LayoutInflater inflater = (LayoutInflater) MapSights.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View pView = inflater.inflate(R.layout.maplusbtnadds, null, false);
    View pViewSight = inflater.inflate(R.layout.add_sighting, null, false);
    tvCategory = (TextView) pViewSight.findViewById(R.id.txt_category);


    pw = new PopupWindow(pView, 450, 650, false);
    add_sg = new PopupWindow(pViewSight, 450, 650, false);


    btnPlus = (ImageView) findViewById(R.id.plusBtn);
    close_add_sight = (ImageView) pView.findViewById(R.id.close_plus);

    btnPlus.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            pw.showAtLocation(MapSights.this.findViewById(R.id.map), Gravity.BOTTOM | Gravity.RIGHT, 0, 0);
        }
    });

    close_add_sight.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            pw.dismiss();
        }
    });

    add_sight_btn = (LinearLayout) pView.findViewById(R.id.add_sight_btn);
    add_sight_btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            add_sg.showAtLocation(MapSights.this.findViewById(R.id.map), Gravity.BOTTOM | Gravity.RIGHT, 0, 0);
        }

    });


    tvCategory.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            showList(v);
        }
    });

}


@Override
protected void onResume() {
    super.onResume();
    Log.d(TAG,"onResume");
}


@Override
protected void onStart() {
    super.onStart();
    Log.d(TAG,"onStart");
}


@Override
protected void onStop() {
    super.onStop();
    Log.d(TAG,"onStop");
}


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) {
            Toast.makeText(MapSights.this, "Clicked item " + position, Toast.LENGTH_SHORT).show();
            tvCategory.setText(Category[position]);
            popup.dismiss();
        }
    });

    popup.show();


}


@Override
public void onMapReady(GoogleMap map) {

// map.addMarker(new MarkerOptions()。position(new LatLng(0,0))。title(“Marker”));         map.setMapType(MAP_TYPE_TERRAIN);     }

}

1 个答案:

答案 0 :(得分:0)

更改final ListPopupWindow popup = new ListPopupWindow(this);final ListPopupWindow popup = new ListPopupWindow(MapSights.this);

尝试在致电popup.show();之前检查活动是否完成,以避免错误您的活动是否正在运行?

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