android.view.WindowManager$BadTokenException list

2020-03-28 22:18发布

问题:

This is my code:

        LayoutInflater layoutInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        LinearLayout viewGroup = (LinearLayout) findViewById(R.id.layout_popup);
        final View layout = layoutInflater.inflate(R.layout.popup_layout,viewGroup);

        final PopupWindow popup = new PopupWindow(GameActivity.this);

        Point popupSize = new Point(getResources().getDimensionPixelSize(R.dimen.popup_width),getResources().getDimensionPixelSize(R.dimen.popup_height));


        popup.setContentView(layout);
        popup.setWidth(popupSize.x);
        popup.setHeight(popupSize.y);
        popup.setFocusable(true);

        File dir = getFilesDir();
        File temp = new File(dir.getPath()+"/temp.prj");
        try {
            if(!temp.exists())temp.createNewFile();
        } catch (IOException e) {
            e.printStackTrace();
        }

        File[] filesArray = dir.listFiles();
        ArrayList<String> files = new ArrayList<>();

        for(File file:filesArray)
        {
            files.add(file.getName());
        }

        Display display = getWindowManager().getDefaultDisplay();
        Point size = new Point();
        display.getSize(size);

        popup.showAtLocation(layout, Gravity.NO_GRAVITY, (size.x-popupSize.x)/2, (size.y-popupSize.y)/2);


        ListView listView = (ListView)layout.findViewById(R.id.listViewDir);
        final EditText fileName = (EditText)layout.findViewById(R.id.editTextFilename);

        final StableArrayAdapter adapter = new StableArrayAdapter(GameActivity.this,R.layout.list_item, files);

        listView.setAdapter(adapter);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, final View view, int position, long id) {
                final TextView item = (TextView) view;
                fileName.setText(item.getText());
            }
        });

The bit that is causing me trouble is:

            @Override
            public void onItemClick(AdapterView<?> parent, final View view, int position, long id) {
                final TextView item = (TextView) view;
                fileName.setText(item.getText());
            }

Where after I have clicked on the item in the list view, I then go to click on the filename view and the exception

 android.view.WindowManager$BadTokenException: Unable to add window -- token android.view.ViewRootImpl$W@53668b30 is not valid; is your activity running?

is thrown. I have tried running fileName.setText(item.getText()); on the UI thread, I have tried using MainActivity.this (or my variant of it, GameActivity.this) in numerous different places. Nothing seems to be working. Does anyone have any idea what's going wrong? All related questions seem to revolve around a Dialog rather than a Popup


UPDATE

For some unearthly reason, adding android:inputType="textNoSuggestions" to the EditText fileName in xml stops the exception. I only noticed this after prefixing the items in the listview with a number (which means they don't get spellchecked). It looks like someone else ran into a similar error Android ICS spelling error correction causes PopupWindow to crash and they too do not have an answer