我有一个使用异步任务来处理Web服务消耗的活动,但是当我进入活动或单击其中一个按钮时出现错误,似乎ProgressDialog没有关闭,我在这里错过了什么?任何想法都会有所帮助!
错误日志:
11-28 08:24:28.589: E/WindowManager(20301): android.view.WindowLeaked: Activity com.roneskinder.selfierush.activity.TaskFactoryRateTasksActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView{45400170 G.E..... R.....ID 0,0-180,180} that was originally added here
11-28 08:24:28.589: E/WindowManager(20301): at android.view.ViewRootImpl.<init>(ViewRootImpl.java:436)
11-28 08:24:28.589: E/WindowManager(20301): at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:267)
11-28 08:24:28.589: E/WindowManager(20301): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69)
11-28 08:24:28.589: E/WindowManager(20301): at android.app.Dialog.show(Dialog.java:288)
11-28 08:24:28.589: E/WindowManager(20301): at com.roneskinder.selfierush.login.util.CustomProgressDialog.show(CustomProgressDialog.java:41)
11-28 08:24:28.589: E/WindowManager(20301): at com.roneskinder.selfierush.activity.TaskFactoryRateTasksActivity$asyncGetTaskClass.onPreExecute(TaskFactoryRateTasksActivity.java:84)
11-28 08:24:28.589: E/WindowManager(20301): at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:587)
11-28 08:24:28.589: E/WindowManager(20301): at android.os.AsyncTask.execute(AsyncTask.java:535)
11-28 08:24:28.589: E/WindowManager(20301): at com.roneskinder.selfierush.activity.TaskFactoryRateTasksActivity.onCreate(TaskFactoryRateTasksActivity.java:44)
11-28 08:24:28.589: E/WindowManager(20301): at android.app.Activity.performCreate(Activity.java:5389)
11-28 08:24:28.589: E/WindowManager(20301): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
11-28 08:24:28.589: E/WindowManager(20301): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2256)
11-28 08:24:28.589: E/WindowManager(20301): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2350)
11-28 08:24:28.589: E/WindowManager(20301): at android.app.ActivityThread.access$800(ActivityThread.java:163)
11-28 08:24:28.589: E/WindowManager(20301): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1257)
11-28 08:24:28.589: E/WindowManager(20301): at android.os.Handler.dispatchMessage(Handler.java:102)
11-28 08:24:28.589: E/WindowManager(20301): at android.os.Looper.loop(Looper.java:157)
11-28 08:24:28.589: E/WindowManager(20301): at android.app.ActivityThread.main(ActivityThread.java:5335)
11-28 08:24:28.589: E/WindowManager(20301): at java.lang.reflect.Method.invokeNative(Native Method)
11-28 08:24:28.589: E/WindowManager(20301): at java.lang.reflect.Method.invoke(Method.java:515)
11-28 08:24:28.589: E/WindowManager(20301): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
11-28 08:24:28.589: E/WindowManager(20301): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
11-28 08:24:28.589: E/WindowManager(20301): at dalvik.system.NativeStart.main(Native Method)
11-28 08:24:28.589: E/WindowManager(20301): android.view.WindowLeaked: Activity com.roneskinder.selfierush.activity.TaskFactoryRateTasksActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView{45428730 G.E..... R.....ID 0,0-180,180} that was originally added here
11-28 08:24:28.589: E/WindowManager(20301): at android.view.ViewRootImpl.<init>(ViewRootImpl.java:436)
11-28 08:24:28.589: E/WindowManager(20301): at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:267)
11-28 08:24:28.589: E/WindowManager(20301): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69)
11-28 08:24:28.589: E/WindowManager(20301): at android.app.Dialog.show(Dialog.java:288)
11-28 08:24:28.589: E/WindowManager(20301): at com.roneskinder.selfierush.login.util.CustomProgressDialog.show(CustomProgressDialog.java:41)
11-28 08:24:28.589: E/WindowManager(20301): at com.roneskinder.selfierush.activity.TaskFactoryRateTasksActivity$asyncRateClass.onPreExecute(TaskFactoryRateTasksActivity.java:156)
11-28 08:24:28.589: E/WindowManager(20301): at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:587)
11-28 08:24:28.589: E/WindowManager(20301): at android.os.AsyncTask.execute(AsyncTask.java:535)
11-28 08:24:28.589: E/WindowManager(20301): at com.roneskinder.selfierush.activity.TaskFactoryRateTasksActivity.asyncRateTask(TaskFactoryRateTasksActivity.java:64)
11-28 08:24:28.589: E/WindowManager(20301): at com.roneskinder.selfierush.activity.TaskFactoryRateTasksActivity$2.onClick(TaskFactoryRateTasksActivity.java:58)
11-28 08:24:28.589: E/WindowManager(20301): at android.view.View.performClick(View.java:4630)
11-28 08:24:28.589: E/WindowManager(20301): at android.view.View$PerformClick.run(View.java:19339)
11-28 08:24:28.589: E/WindowManager(20301): at android.os.Handler.handleCallback(Handler.java:733)
11-28 08:24:28.589: E/WindowManager(20301): at android.os.Handler.dispatchMessage(Handler.java:95)
11-28 08:24:28.589: E/WindowManager(20301): at android.os.Looper.loop(Looper.java:157)
11-28 08:24:28.589: E/WindowManager(20301): at android.app.ActivityThread.main(ActivityThread.java:5335)
11-28 08:24:28.589: E/WindowManager(20301): at java.lang.reflect.Method.invokeNative(Native Method)
11-28 08:24:28.589: E/WindowManager(20301): at java.lang.reflect.Method.invoke(Method.java:515)
11-28 08:24:28.589: E/WindowManager(20301): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
11-28 08:24:28.589: E/WindowManager(20301): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
11-28 08:24:28.589: E/WindowManager(20301): at dalvik.system.NativeStart.main(Native Method)
活动:
package com.roneskinder.selfierush.activity;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import com.roneskinder.selfierush.login.R;
import com.roneskinder.selfierush.login.util.CustomProgressDialog;
import com.roneskinder.selfierush.login.util.UserFunctions;
public class TaskFactoryRateTasksActivity extends Activity {
public static final String TAG_GETTASK = "getRateTask";
public static final String TAG_RATETASK = "RateTask";
protected TextView taskDescription, taskDescriptionExtra, taskScore;
protected Button btn_like, btn_dislike;
public String idtask, description, description_extra, score, rate, message, response;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.task_factory_ratetask_layout);
taskDescription = (TextView) findViewById(R.id.taskDescription);
taskDescriptionExtra = (TextView) findViewById(R.id.taskDescriptionExtra);
taskScore = (TextView) findViewById(R.id.taskScore);
btn_like = (Button) findViewById(R.id.btn_like);
btn_dislike = (Button) findViewById(R.id.btn_dislike);
new asyncGetTaskClass(this).execute();
// Like
btn_like.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
rate = "1";
asyncRateTask(v);
}
});
// Like
btn_dislike.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
rate = "0";
asyncRateTask(v);
}
});
}
public void asyncRateTask(View v) {
new asyncRateClass(this).execute();
}
public void setTextViews(){
taskDescription.setText(description);
taskDescriptionExtra.setText(description_extra);
taskScore.setText(score);
}
class asyncGetTaskClass extends AsyncTask<Void, Void, Void> {
private final ProgressDialog progressDialog;
public asyncGetTaskClass(Context ctx) {
progressDialog = CustomProgressDialog.ctor(ctx);
}
@Override
protected void onPreExecute() {
super.onPreExecute();
progressDialog.show();
}
@Override
protected Void doInBackground(Void... params) {
// sleep for 5 seconds
UserFunctions userFunction = new UserFunctions();
JSONObject json = userFunction.getRateTask();
try {
// news found
// Getting Array of News
JSONArray emailLogin = json.getJSONArray(TAG_GETTASK);
// Log.d("JSON", "NEWS: " + news);
if (emailLogin != null) {
// looping through All News
for (int i = 0; i < emailLogin.length(); i++) {
JSONObject c = emailLogin.getJSONObject(i);
// Storing each json item in variable
idtask = c.getString("idtask");
description = c.getString("description");
description_extra = c.getString("description_extra");
score = c.getString("score");
Log.d("TASK INFO", "Task:" + idtask + "\ndescription:" + description + "\ndescription_extra:" + idtask + "\nscore:" + score);
}
} else {
// Check your log cat for JSON reponse
Log.d("Login empty: ", json.toString());
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
try {
if (null != progressDialog && progressDialog.isShowing()) {
progressDialog.hide();
}
setTextViews();
} catch (Exception e) {
e.printStackTrace();
}
}
}
class asyncRateClass extends AsyncTask<Void, Void, Void> {
private final ProgressDialog progressDialog;
public asyncRateClass(Context ctx) {
progressDialog = CustomProgressDialog.ctor(ctx);
}
@Override
protected void onPreExecute() {
super.onPreExecute();
progressDialog.show();
}
@Override
protected Void doInBackground(Void... params) {
// sleep for 5 seconds
UserFunctions userFunction = new UserFunctions();
JSONObject json = userFunction.RateTask(rate, idtask);
try {
// news found
// Getting Array of News
JSONArray emailLogin = json.getJSONArray(TAG_RATETASK);
// Log.d("JSON", "NEWS: " + news);
if (emailLogin != null) {
// looping through All News
for (int i = 0; i < emailLogin.length(); i++) {
JSONObject c = emailLogin.getJSONObject(i);
// Storing each json item in variable
response = c.getString("response");
message = c.getString("message");
Log.d("TASK INFO", "response:" + response + "\nmessage:" + message);
}
} else {
// Check your log cat for JSON reponse
Log.d("Login empty: ", json.toString());
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
try {
if (null != progressDialog && progressDialog.isShowing()) {
progressDialog.hide();
}
if(response.equals("1")){
Intent intent = new Intent(getBaseContext(), TaskFactoryMenuActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.putExtra("response", "2");
startActivity(intent);
}
else{
Intent intent = new Intent(getBaseContext(), TaskFactoryMenuActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.putExtra("response", "3");
startActivity(intent);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
答案 0 :(得分:2)
您需要拨打progressDialog.dismiss()
而不是progressDialog.hide()
。两者之间存在细微差别:
dismiss()
:
关闭此对话框,将其从屏幕上删除。
hide()
:
隐藏对话框,但不要忽略它。
当你不再需要对话时,你不要将它隐藏在床底下,你会被解雇而把它赶走。 :P