我在这里遇到过类似的问题,但无法解决问题。
我知道在退出活动后我正在显示进度条,但我不明白我的代码在哪里出错,我在onPreExecute
显示进度条并在onPostExecute
中将其解雇。
CODE
private class NetCheck extends AsyncTask <Void, Void, Boolean>{
private ProgressDialog nDialog;
@Override
protected void onPreExecute() {
super.onPreExecute();
nDialog = new ProgressDialog(Login.this);
nDialog.setTitle("Checking Network");
nDialog.setMessage("Loading..");
nDialog.setIndeterminate(false);
nDialog.setCancelable(true);
nDialog.show();
}
@Override
protected Boolean doInBackground(Void... params) {
ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo nf = cm.getActiveNetworkInfo();
if(nf != null && nf.isConnected()) {
try {
URL url = new URL("http://www.google.com");
HttpURLConnection urlc = (HttpURLConnection) url.openConnection();
urlc.setConnectTimeout(5000);
urlc.connect();
if(urlc.getResponseCode() == 200) {
return true;
}
}
catch(MalformedURLException e1) {
e1.printStackTrace();
}
catch(IOException e2) {
e2.printStackTrace();
}
}
return false;
}
@Override
protected void onPostExecute(Boolean th) {
if(th == true) {
nDialog.dismiss();
new ProcessLogin().execute();
}
else{
nDialog.dismiss();
errorMSG.setText("Error in Network Connection");
}
}
}
错误日志
E/WindowManager: android.view.WindowLeaked: Activity boxyworldreseller.com.boxyworldreseller.Login has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView{322c8153 V.E..... R......D 0,0-1026,483} that was originally added here
at android.view.ViewRootImpl.<init>(ViewRootImpl.java:363)
at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:271)
at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:85)
at android.app.Dialog.show(Dialog.java:298)
at boxyworldreseller.com.boxyworldreseller.Login$NetCheck.onPreExecute(Login.java:92)
at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:591)
at android.os.AsyncTask.execute(AsyncTask.java:539)
at boxyworldreseller.com.boxyworldreseller.Login.NetAsync(Login.java:77)
at boxyworldreseller.com.boxyworldreseller.Login$3.onClick(Login.java:68)
at android.view.View.performClick(View.java:4780)
at android.view.View$PerformClick.run(View.java:19866)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
答案 0 :(得分:0)
protected void onPreExecute() {
super.onPreExecute();
if(!isFinishing() && this != null){
nDialog = new ProgressDialog(Login.this);
nDialog.setTitle("Checking Network");
nDialog.setMessage("Loading..");
nDialog.setIndeterminate(false);
nDialog.setCancelable(true);
nDialog.show();
}
}
在Showin进度对话框之前检查这两个条件如果你正在使用活动,如果你正在使用片段,那么使用如下所示:
protected void onPreExecute() {
super.onPreExecute();
if(!getActivity().isFinishing() && getActivity()!= null)
nDialog = new ProgressDialog(Login.this);
nDialog.setTitle("Checking Network");
nDialog.setMessage("Loading..");
nDialog.setIndeterminate(false);
nDialog.setCancelable(true);
nDialog.show();
}
}