我正在开发一个弹出窗口,Android和它的工作,我加了的EditText和一个按钮,在ADV运行时,这个正常工作,而在设备上运行,当我专注于EditText上,这将引发一个奇怪例外。
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?
我不知道它的问题,但我对Galaxy Tab的运行与Swype输入法。
I don't know if it matters, but I'm running on a Galaxy Tab with Swype input.
现在我读Window.showAtLocation的规格
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?
我也写了一个小code重现错误。
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);
运行在AVD一切工作正常,而在手机本坠毁并引发我提到的错误。
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.
推荐答案
我试着运行code,但一切工作正常,我......下面是测试类我写的:
I tried to run your code but everything works fine for me... Here is the test class I wrote :
public class TestActivity extends Activity
{
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button btn = (Button) findViewById(R.id.testBtn);
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v)
{
showPopup();
}
});
}
private void showPopup()
{
PopupWindow window = new PopupWindow(this);
window.setWidth(WindowManager.LayoutParams.WRAP_CONTENT);
window.setHeight(WindowManager.LayoutParams.WRAP_CONTENT);
window.setTouchable(true);
window.setFocusable(true);
EditText text = new EditText(this);
text.setText("Touch it, it doesn't crash");
window.setContentView(text);
window.showAtLocation(text, Gravity.NO_GRAVITY, 30, 30);
}
}
main.xml中:
main.xml :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<Button
android:id="@+id/testBtn"
android:text="Popup"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
也许你试图运行弹出code在OnCreate()函数?如果我这样做,它抛出同样的异常是你的,但它是正常的,因为当的onCreate()被调用的活动没有完全初始化呢。
Maybe you tried to run the popup code in the onCreate() function? If I do it, it throws the same Exception as yours, but it's normal since when onCreate() is called the activity is not fully initialized yet.
(我试过在Galaxy Tab的太多,但没有Swype输入法)
(I tried on a Galaxy Tab too, but without swype input)
相关推荐
最新文章