显示PopupWindow时出现以下错误。 这些错误由以下行触发:
checkInPopup.showAtLocation((ViewGroup) mapView.getParent(), Gravity.CENTER_HORIZONTAL, 0, 0);
mapView是一个MapView,没有任何东西是空的。 stacktrace:
01-08 18:00:09.402: E/AndroidRuntime(27768): Caused by: android.view.WindowManager$BadTokenException: Unable to add window -- token null is not valid; is your activity running?
01-08 18:00:09.402: E/AndroidRuntime(27768): at android.view.ViewRootImpl.setView(ViewRootImpl.java:513)
01-08 18:00:09.402: E/AndroidRuntime(27768): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:301)
01-08 18:00:09.402: E/AndroidRuntime(27768): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:215)
01-08 18:00:09.402: E/AndroidRuntime(27768): at android.view.WindowManagerImpl$CompatModeWrapper.addView(WindowManagerImpl.java:140)
01-08 18:00:09.402: E/AndroidRuntime(27768): at android.view.Window$LocalWindowManager.addView(Window.java:537)
01-08 18:00:09.402: E/AndroidRuntime(27768): at android.widget.PopupWindow.invokePopup(PopupWindow.java:988)
01-08 18:00:09.402: E/AndroidRuntime(27768): at android.widget.PopupWindow.showAtLocation(PopupWindow.java:845)
01-08 18:00:09.402: E/AndroidRuntime(27768): at android.widget.PopupWindow.showAtLocation(PopupWindow.java:809)
01-08 18:00:09.402: E/AndroidRuntime(27768): at com.geoloc.ActivityCheckIn.onCreate(ActivityCheckIn.java:50)
01-08 18:00:09.402: E/AndroidRuntime(27768): at android.app.Activity.performCreate(Activity.java:4465)
01-08 18:00:09.402: E/AndroidRuntime(27768): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
这是我活动的代码(扩展了MapActivity)
protected void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.checkin);
mapView = (MapView) findViewById(R.id.mapview);
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
checkInPopup = new PopupWindow(inflater.inflate(CHECK_IN_POPUP_LAYOUT, null, false));
checkInPopup.setOutsideTouchable(true);
checkInPopup.setHeight(100);
checkInPopup.setWidth(200);
checkInPopup.showAtLocation((ViewGroup) mapView.getParent(), Gravity.CENTER_HORIZONTAL, 0, 0);
}
感谢您分享您的想法
答案 0 :(得分:78)
当我尝试在活动中显示弹出菜单时,我也遇到同样的问题,但是我遇到问题并通过提供上下文来解决问题
YourActivityName.this
代替<{1}}
getApplicationContext()
是的,它对我有用,可能会帮助别人
答案 1 :(得分:69)
你过早地展示你的弹出窗口。您可以在Onresume上发布延迟的runnable for showatlocation,试一试
修改强> 这篇文章似乎有同样的问题回答Problems creating a Popup Window in Android Activity
答案 2 :(得分:20)
使用:
df['environment'] = np.where(pd.isnull(df['environment']), df['environment'] = 'RD', df['environment'])
SyntaxError: keyword can't be an expression
代替:
for(Persona persona : personas) {
if(persona instanceof Profesor) {
profesores.add((Profesor) persona);
}
else if(persona instanceof Alumno) {
alumnos.add((Alumno) persona);
}
else {
nuevasPersonas.add(persona);
}
}
答案 3 :(得分:10)
发生此异常时有两种情况。 nandeesh提到了一个。这里提到了其他方案:http://blackriver.to/2012/08/android-annoying-exception-unable-to-add-window-is-your-activity-running/
确保你同时处理它们
答案 4 :(得分:3)
@Override
protected void onCreate(Bundle savedInstanceState) {
View view = LayoutInflater.from(mContext).inflate(R.layout.popup_window_layout, new LinearLayout(mContext), true);
popupWindow = new PopupWindow(view, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
popupWindow.setContentView(view);
}
@Override
public void onWindowFocusChanged(boolean hasFocus) {
if (hasFocus) {
popupWindow.showAtLocation(parentView, Gravity.BOTTOM, 0, 0);
}
}
正确的方法是onWindowFocusChanged()的popupwindow.show()。
答案 5 :(得分:2)
弹出窗口的父级本身不能成为弹出式窗口。他们的父母都必须是一样的。因此,如果您在弹出窗口中创建弹出窗口,则必须保存父窗口的弹出窗口并使其成为父窗口。
这是example
答案 6 :(得分:1)
尝试显示下面的流行
findViewById(R.id.main_layout).post(new Runnable() {
public void run() {
mPopupWindow.showAtLocation(findViewById(R.id.main_layout), Gravity.CENTER, 0, 0);
Button close = (Button) customView.findViewById(R.id.btn_ok);
close.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mPopupWindow.dismiss();
doOtherStuff();
}
});
}
});
答案 7 :(得分:0)
尝试使用它
LayoutInflater inflater = (LayoutInflater).getApplicationContext().getSystemService(LAYOUT_INFLATER_SERVICE);
View view = inflate.from(YourActivity.this).inflate(R.layout.yourLayout, null);
答案 8 :(得分:0)
在演出前使用此方法
public static ViewGroup getActivityFirstLayout(Context ctx)
{
return (ViewGroup)((ViewGroup) ActivityMaster.getActivity(ctx).findViewById(android.R.id.content)).getChildAt(0);
}
private boolean activityIsOk(Activity activity)
{
boolean s1 = !(activity == null || activity.isFinishing());
if(s1)
{
View v = LayoutMaster.getActivityFirstLayout(activity);
return v.isShown() && ViewCompat.isLaidOut(v);
}
return false;
}
答案 9 :(得分:0)
经过数小时的搜索和测试,我发现以下解决方案(通过实施不同的SO解决方案)在任何情况下都不会失败,而我失败了。
Runnable runnable = new Runnable() {
@Override
public void run() {
//displayPopup,progress dialog or what ever action. example
ProgressDialogBox.setProgressBar(Constants.LOADING,youractivityName.this);
}};
logcat指示崩溃正在发生..就我而言,在接收广播时开始运行。
runOnUiThread(new Runnable() {
@Override
public void run() {
if(!isFinishing()) {
new Handler().postAtTime(runnable,2000);
}
}
});
答案 10 :(得分:-1)
尝试显示这样的弹出窗口
new Handler().postDelayed(new Runnable(){
public void run() {
popupWindow.showAtLocation(context.getWindow().getDecorView(), Gravity.CENTER,0,0);
}
}, 200L);