gpt4 book ai didi

android - activity.isFinishing 语句中的 BadTokenException : Unable to add window -- token null is not valid; is your activity running?

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:01:13 52 4
gpt4 key购买 nike

如果用户仍在当前 Activity 中,我有一个需要显示的弹出窗口,几秒钟后。我实现了 stament 检查 Activity 是否未完成/销毁然后显示弹出窗口,它工作正常,周末用户 :)(从一个 Activity 慢慢点击到另一个 Activity )但在高压测试中( Activity 正在重新创建,完成,快速移动形式 Activity 到 Activity )给我那个错误:

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()

尝试将 showPopUpWindow() 中的 runnable 代码替换为:

runOnUiThread(new Runnable() {
@Override
public void run() {
if (!isFinishing()) {
popupWindow.showAtLocation(context.getWindow().getDecorView(), Gravity.CENTER, 0, 0);
}
}
});

关于android - activity.isFinishing 语句中的 BadTokenException : Unable to add window -- token null is not valid; is your activity running?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41675828/

52 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com