Exception when focusing a EditText in a PopupWindo

2020-06-08 16:39发布

I'm developing a PopUp window for Android, and it's working, I added a EditText and a Button on that, when running on ADV this work properly, while running on device, when I focus on the EditText this throws a weird Exception.

android.view.WindowManager$BadTokenException: Unable to add window - - token android.view.ViewRoot&48163b18 is not valid; is your active running?

I don't know if it matters, but I'm running on a Galaxy Tab with Swype input.

Now I read the specs of the Window.showAtLocation

public void showAtLocation (View parent, int gravity, int x, int y)

Display the content view in a popup window at the specified location. If the popup window cannot fit on screen, it will be clipped. [...]

Parameters
parent  a parent view to get the getWindowToken() token from
[...]

The problem is just in that token, but how do I pass the Activity token to it?

I also wrote a small code to reproduce the error.

PopupWindow window = new PopupWindow(activity);
window.setWidth(WindowManager.LayoutParams.WRAP_CONTENT);
window.setHeight(WindowManager.LayoutParams.WRAP_CONTENT);

window.setTouchable(true);
window.setFocusable(true);

EditText text = new EditText(activity);
text.setText("Dont touch, this crash!");

window.setContentView(text);
window.showAtLocation(arg0, Gravity.NO_GRAVITY, 10,10);

Running on AVD all works fine, while on device this crash and throw the error I mentioned.

I discover something new, when I'm in landscape mode this errors don't occurs.

7条回答
\"骚年 ilove
2楼-- · 2020-06-08 17:39
EditText text = new EditText(activity);
text.setText("Dont touch, this crash!");

this actually is the cause of exception..

when we bind the EditText to some text it falls out on click when virtual keyboard appears..

But the same code works fine when we do not bind the EditText

EditText text = new EditText(activity);

Although I am facing the same error, and not able to sort out till now....

Just putting efforts here to focus on problem creater line...

Can any body suggest why itz behaving such and how to fix this issue..

Thankx

查看更多
登录 后发表回答