我试图开发一个应用程序但是当我触摸BACK按钮时系统显示无法添加窗口 - 令牌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);
}
}
请帮助我,因为我的工作,我什么都不知道......
答案 0 :(得分:1)
问题出在AsyncTask类中,因为&#34; if&#34;不对。使用此代码可以正常工作。
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();
}
}