【问题标题】:Unable to add window - token android.os.BinderProxy@41b70320 is not valid; is your activity running?无法添加窗口 - 令牌 android.os.BinderProxy@41b70320 无效;您的活动正在运行吗?
【发布时间】:2025-12-31 13:50:11
【问题描述】:

我正在尝试开发应用程序,但当我触摸返回按钮时,系统显示无法添加窗口 - 令牌 android.os.BinderProxy@41b70320 无效;您的活动正在运行吗? 我不明白,我该如何解决? 我的对话框是用 AsyncTask 创建的 我的代码: 类(不完整,因为很大)

 public class VistaJuego extends View {


Drawable drawableFaba,drawableFabaMinera,drawableFabaAstur, drawableBotellasAbajo,drawableBotellasArriba,aux,botellaRota;
Grafico faba;
Vector<Grafico> botellasAbajo,botellasArriba;
int numeroBotellas = 2;
static int PERIODO_PROCESO = 50;
ThreadJuego juego = new ThreadJuego();
ThreadFaba hiloFaba= new ThreadFaba();
int alto,ancho,altoPantalla;//alto del canvas
MainActivity main;
Boolean ifmusica,ifvibracion;
Activity activity;
Async a;
public void gestionarChoque(Grafico elementofaba,Grafico elementoBotella){

    boolean guardado=false;
    if((elementofaba.getPosX()+elementofaba.getAncho()>=elementoBotella.getPosX()+15)){
        juego.detener();
        hiloFaba.detener();
        if(ifmusica==true){
            sonidoJuego.stop();
            golpe.start();
        }
        if(ifvibracion==true){
            v.vibrate(200);
        }


        elementoBotella.setDrawable(botellaRota);
        if(guardado==false){
            bd.guardarPuntuacion(marcador.getPuntos()+1, faba.hacerFecha());
            guardado=true;
        }   

        a= new Async((Activity) getContext());
        a.execute();

        Log.i("Guardó de VistaJuego", "guardado abajo");
    }

和活动

public class AngryJuego extends Activity {

RelativeLayout relative;
public int anchoPantalla, altoPantalla;
VistaJuego vistafaba;
SharedPreferences sharedPref;
Boolean brillo;


@Override
protected void onCreate(Bundle savedInstanceState) {
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
    super.onCreate(savedInstanceState);
    setContentView(R.layout.angry_juego);
    WindowManager.LayoutParams layout = getWindow().getAttributes();
    Display display = getWindowManager().getDefaultDisplay();
    altoPantalla=display.getHeight();

    RelativeLayout relative = (RelativeLayout)findViewById(R.id.relative);
    vistafaba=(VistaJuego)findViewById(R.id.faba);
    relative=(RelativeLayout)findViewById(R.id.relative);
    vistafaba.setAltoPantalla(altoPantalla);
    }


}

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if ((keyCode == KeyEvent.KEYCODE_BACK)){


        Intent i= new Intent(this,MainActivity.class);
        finish();
        startActivity(i);


    }

    return super.onKeyDown(keyCode, event);
}

}

请帮帮我是为了我的工作,我什么都不明白....

【问题讨论】:

  • 如何实例化对话框?很可能您的对话框是在应用程序关闭时创建的,因此父 Activity 已经被销毁,并且应用程序无法找到对话框的正确父项。

标签: android android-asynctask dialog


【解决方案1】:

问题出在 AsyncTask 类中,因为“if”不正确。使用此代码,它可以工作。

public class Async extends AsyncTask<Void,Void,Void>{


    AlertDialog.Builder builder;
    Activity contexto;
    WeakReference<Activity> activityWeakRef;

    public Async(Activity context) {
        activityWeakRef = new WeakReference<Activity>(context);
        contexto=context;
    }

    @Override
    protected Void doInBackground(Void... params) {

        return null;

    }

    @Override
    protected void onPreExecute() {
        super.onPreExecute();

    }

    @Override
    protected void onPostExecute(Void result) {
        super.onPostExecute(result);
        if (activityWeakRef != null  && !activityWeakRef.get().isFinishing()) {
            builder = new AlertDialog.Builder(activityWeakRef.get());
                    .setPositiveButton(R.string.si,
                            new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog,
                                        int id) {
                                    Intent i= contexto.getIntent();
                                    contexto.finish();
                                    contexto.startActivity(i);

                                }
                            })
                    .setNegativeButton(R.string.no,
                            new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog,
                                        int id) {

                                    Intent i= new Intent(contexto,MainActivity.class);
                                    contexto.startActivity(i);
                                }
                            });
            builder.create().show();
        }
    }

【讨论】:

    最近更新 更多