BadTokenException:无法添加窗口-令牌null无效;您的活动正在进行吗?在活动中 [英] BadTokenException: Unable to add window -- token null is not valid; is your activity running? in activity.isFinishing statment
问题描述
如果用户仍处于当前活动状态,则在几秒钟后,我需要显示一个popUpWindow.我实施了stament,检查活动是否未完成/销毁,然后显示Popup,并且对于周末用户而言,它可以正常工作:)(在活动之间缓慢单击),但在高压力测试中(活动正在重新创建,完成,快速移动)活动到活动),这给了我这个错误:
I have a popUpWindow that need to by displayed, after couple of seconds if the user is still on current activity. I implemented stament that check if the activity isnt finished/destroyed and then display the Popup, and it works fine, for weekend users :) ( slowly clicking from activity to activity) but in high pressuretests ( activities are recreating, finished, fast move form activity to activity) that gives me that error :
E/UncaughtException:android.view.WindowManager $ BadTokenException: 无法添加窗口-令牌null无效;是你的活动 跑步? 在android.view.ViewRootImpl.setView(ViewRootImpl.java:598) 在 android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:341) 在android.view.WindowManagerImpl.addView(WindowManagerImpl.java:85) 在android.widget.PopupWindow.invokePopup(PopupWindow.java:1279) 在android.widget.PopupWindow.showAtLocation(PopupWindow.java:1040) 在android.widget.PopupWindow.showAtLocation(PopupWindow.java:1003) 在com.guides4art.app.ImageSlider.RatePopUp $ 3.run(RatePopUp.java:86) 在android.os.Handler.handleCallback(Handler.java:743) 在android.os.Handler.dispatchMessage(Handler.java:95) 在android.os.Looper.loop(Looper.java:150) 在android.app.ActivityThread.main(ActivityThread.java:5546) 在java.lang.reflect.Method.invoke(本机方法) 在 com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:794) 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:684)
E/UncaughtException: android.view.WindowManager$BadTokenException: Unable to add window -- token null is not valid; is your activity running? at android.view.ViewRootImpl.setView(ViewRootImpl.java:598) at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:341) at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:85) at android.widget.PopupWindow.invokePopup(PopupWindow.java:1279) at android.widget.PopupWindow.showAtLocation(PopupWindow.java:1040) at android.widget.PopupWindow.showAtLocation(PopupWindow.java:1003) at com.guides4art.app.ImageSlider.RatePopUp$3.run(RatePopUp.java:86) at android.os.Handler.handleCallback(Handler.java:743) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:150) at android.app.ActivityThread.main(ActivityThread.java:5546) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:794) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:684)
代码:
private void showPopUpWindow(final Activity context){
popupWindow = new PopupWindow(context);
LinearLayout.LayoutParams layoutParams =new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
popupWindow.setHeight(layoutParams.height);
popupWindow.setWidth(layoutParams.width);
popupWindow.setOutsideTouchable(true);
popupWindow.setTouchable(true);
popupWindow.setFocusable(true);
popupWindow.setContentView(view);
ratingBar.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener() {
@Override
public void onRatingChanged(RatingBar ratingBar, float rating, boolean fromUser) {
if(context instanceof CarSale) {
((CarSale) context).saveRate((int) rating);
((CarSale) context).initRate();
title.setText(""+context.getString(R.string.thanksForRate));
}
else
Log.i("kamil","error");
}
});
closeButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
popupWindow.dismiss();
}
});
if(!context.isFinishing() || !context.isDestroyed() )
activityView.post(new Runnable() {
@Override
public void run() {
popupWindow.showAtLocation(context.getWindow().getDecorView(), Gravity.CENTER,0,0);
}
});
}
//View Pager Class
@Override
public void onPageSelected(int position) {
if(viewPager !=null){
this.position=position;
if(position==carList.size()-1 && isRated() && showRateBar)
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
new RatePopUp(Cars.this,activityView);
showRateBar=false;
}
},5*SECOND);
//RatePopUp constructor
public RatePopUp(Activity context,View activityView){
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.rate_popup_layout, null);
this.activityView=activityView;
ratingBar = (RatingBar) view.findViewById(R.id.ratingPop);
title= (TextView)view.findViewById(R.id.rateTitle);
title.setText(context.getString(R.string.rate_exhibition));
closeButton = (Button)view.findViewById(R.id.close_button);
Typeface typeface =Typeface.createFromAsset(context.getAssets(),"fonts/fontawesome-webfont.ttf");
closeButton.setTypeface(typeface);
closeButton.setText(context.getString(R.string.exitIcon));
showPopUpWindow(context);
}
推荐答案
尝试以下代码:
new Handler().postDelayed(new Runnable(){
public void run() {
popupWindow.showAtLocation(context.getWindow().getDecorView(), Gravity.CENTER,0,0);
}
}, 200L);
代替:
popupWindow.showAtLocation(context.getWindow().getDecorView(), Gravity.CENTER,0,0);
还要确保将ActivityName.this
作为上下文传递..而不是getApplicationContext
()
Also make sure you pass ActivityName.this
as context.. not getApplicationContext
()
尝试用以下代码替换showPopUpWindow()
中的runnable
代码:
Try replace your runnable
code in showPopUpWindow()
with this:
runOnUiThread(new Runnable() {
@Override
public void run() {
if (!isFinishing()) {
popupWindow.showAtLocation(context.getWindow().getDecorView(), Gravity.CENTER, 0, 0);
}
}
});
这篇关于BadTokenException:无法添加窗口-令牌null无效;您的活动正在进行吗?在活动中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!