在下面给出的代码中,我收到错误“在Alertdialog解散之前活动已关闭并且提供活动泄露窗口异常”,并且我在onProgressUpdate中的alert.show中收到异常。解决方案是什么?
String values[] = { Login.uname, Login.pass };
new Communicator().execute(values);
class Communicator extends AsyncTask<String, String, String>
{
int prog = 0;
@Override
protected String doInBackground(String... params) {
String response = null;
try {
if(Login.uname != null && Login.pass != null){
sync(Login.uname, Login.pass);
}else if(SplashScreen.user != null && SplashScreen.pas != null){
sync(SplashScreen.user, SplashScreen.pas);
}
if("ok".equalsIgnoreCase(status))
{
// else{
if(db != null)
{
if (Login.uname != null) {
usernam = Login.uname;
} else if (SplashScreen.user != null) {
usernam = SplashScreen.user;
}
Cursor cursor = db.getAllMixes(SplashScreen.user);
if(cursor!=null && cursor.getCount() > 0)
{
Log.e(">><<", ">>cursor.getCount()<<"+cursor.getCount());
}
}
count ++;
// }
}
totalsize = 0;
for(int i=0; i<list.size(); i++)
{
totalsize += Integer.parseInt(cont.get(i).getSize());
}
} catch (Exception e) {
Log.e("Data.LOG", e.getMessage(), e);
} finally {
}
return response;
}
@Override
protected void onPreExecute() {
prog = 0;
super.onPreExecute();
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
if ("ok".equalsIgnoreCase(status))
{
sizekb=0;
textsync.setText("Syncing mix "+1+"/"+cont.size());
if(cont.size() == 0){
progressBar2.setVisibility(View.INVISIBLE);
progress.setProgress(0);
textsync.setText("No Mixes on Server");
alertdial =new AlertDialog.Builder(NeliteMusicActivity.this).create();
alertdial.setTitle(R.string.set4Play);
alertdial.setMessage(getString(R.string.noMixes));
alertdial.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
alertdial.setButton(getString(R.string.ok),
new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int which) {
alertdial.dismiss();
animatedStartActivity();
}
});
alertdial.setCanceledOnTouchOutside(false);
alertdial.show();
}
}if("tomany".equalsIgnoreCase(status))
{
progress.setVisibility(View.INVISIBLE);
progressBar2.setVisibility(View.INVISIBLE);
textsync.setVisibility(View.INVISIBLE);
AlertDialog alert =new AlertDialog.Builder(NeliteMusicActivity.this).create();
alert.setTitle(R.string.set4Play);
alert.setMessage(getString(R.string.twoDevice));
alert.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
alert.setButton(getString(R.string.ok), new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
animatedStartActivity();
}
});
alert.setCanceledOnTouchOutside(false);
alert.show();
}
if("fail".equalsIgnoreCase(status))
{
progress.setVisibility(View.INVISIBLE);
progressBar2.setVisibility(View.INVISIBLE);
textsync.setVisibility(View.INVISIBLE);
AlertDialog alert =new AlertDialog.Builder(NeliteMusicActivity.this).create();
alert.setTitle(R.string.set4Play);
alert.setMessage(getString(R.string.connectionerror));
alert.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
alert.setButton(getString(R.string.ok), new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
animatedStartActivity();
return;
}
});
alert.setCanceledOnTouchOutside(false);
alert.show();
}
}
@Override
protected void onProgressUpdate(String... values) {
super.onProgressUpdate(values);
int a = 0;
if(!isChange){
a=messag;
progress.setProgress(a);
textsync.setText("Syncing mix "+c+"/"+list.size());
}else{
progressBar2.setVisibility(View.INVISIBLE);
progress.setProgress(0);
textsync.setText("Syncing interrupted");
}
if(progress.getMax()<=a)
{
if (isOnline())
{
if(NeliteMusicActivity.alertShow==true){
final AlertDialog alert =new AlertDialog.Builder(NeliteMusicActivity.this).create();
alert.setTitle(R.string.set4Play);
alert.setMessage(getString(R.string.updatedMix));
alert.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
alert.setButton(getString(R.string.ok),
new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int which) {
alert.dismiss();
animatedStartActivity();
}
});
alert.setCanceledOnTouchOutside(false);
if((alert1 ==null || !alert1.isShowing()) && !alert.isShowing())
alert.show();
}
}
if (!isOnline()) {
if(NeliteMusicActivity.alertShow==true){
final AlertDialog alert =new AlertDialog.Builder(NeliteMusicActivity.this).create();
alert.setTitle(R.string.set4Play);
alert.setMessage(getString(R.string.connectionerror));
alert.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
alert.setButton(getString(R.string.ok), new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
alert.dismiss();
animatedStartActivity();
}
});
alert.setCanceledOnTouchOutside(false);
if(!alert.isShowing())
alert.show();
}
}
}
}
}
logcat的:
12-04 17:50:27.390: E/WindowManager(8190): Activity com.nelitemusic.NeliteMusicActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@40a2cfb8 that was originally added here
12-04 17:50:27.390: E/WindowManager(8190): android.view.WindowLeaked: Activity com.nelitemusic.NeliteMusicActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@40a2cfb8 that was originally added here
12-04 17:50:27.390: E/WindowManager(8190): at android.view.ViewRoot.<init>(ViewRoot.java:291)
12-04 17:50:27.390: E/WindowManager(8190): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:249)
12-04 17:50:27.390: E/WindowManager(8190): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:193)
12-04 17:50:27.390: E/WindowManager(8190): at android.view.WindowManagerImpl$CompatModeWrapper.addView(WindowManagerImpl.java:118)
12-04 17:50:27.390: E/WindowManager(8190): at android.view.Window$LocalWindowManager.addView(Window.java:532)
12-04 17:50:27.390: E/WindowManager(8190): at android.app.Dialog.show(Dialog.java:269)
12-04 17:50:27.390: E/WindowManager(8190): at com.nelitemusic.NeliteMusicActivity$Communicator.onProgressUpdate(NeliteMusicActivity.java:520)
12-04 17:50:27.390: E/WindowManager(8190): at com.nelitemusic.NeliteMusicActivity$Communicator.onProgressUpdate(NeliteMusicActivity.java:1)
12-04 17:50:27.390: E/WindowManager(8190): at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:606)
12-04 17:50:27.390: E/WindowManager(8190): at android.os.Handler.dispatchMessage(Handler.java:99)
12-04 17:50:27.390: E/WindowManager(8190): at android.os.Looper.loop(Looper.java:132)
12-04 17:50:27.390: E/WindowManager(8190): at android.app.ActivityThread.main(ActivityThread.java:4126)
12-04 17:50:27.390: E/WindowManager(8190): at java.lang.reflect.Method.invokeNative(Native Method)
12-04 17:50:27.390: E/WindowManager(8190): at java.lang.reflect.Method.invoke(Method.java:491)
12-04 17:50:27.390: E/WindowManager(8190): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:844)
12-04 17:50:27.390: E/WindowManager(8190): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:602)
12-04 17:50:27.390: E/WindowManager(8190): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:0)
为了在Activity完成时关闭AlertDialog,你可以做的一件事是覆盖onDestroy并在那里将其解除。
@Override
public void onDestroy(){
try{
alert.dismiss();
}
catch(Exception e) {e.printStackTrace();}
super.onDestroy()
}