大家好,我收到以下代码中的错误,任何人都建议我如何解决这个问题......
这是我的代码::
@Override
public void onClick(View v) {
reviewText = etReviews.getText().toString();
if (!TextUtils.isEmpty(reviewText)) {
new ReviewUpdateTask().execute();
} else {
Toast.makeText(getApplicationContext(), "Write the Review",
Toast.LENGTH_SHORT).show();
}
}
private class ReviewUpdateTask extends AsyncTask {
@Override
protected void onPreExecute() {
super.onPreExecute();
dialog = new Dialog(act);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.loader_view);
dialog.show();
}
@Override
protected void onPostExecute(Object result) {
super.onPostExecute(result);
if (dialog.isShowing())
{
dialog.dismiss();
dialog = null;
}
try {
JSONArray arr = AppConstants.reviewInfoObj
.getJSONArray("userReviews");
adapter = new MyReviewAdapter(ReviewActivity.this, arr);
lvReviews.setAdapter(adapter);
} catch (JSONException e) {
e.printStackTrace();
}
}
@Override
protected Object doInBackground(Object... params) {
String response = Data.postData(obj, AppConstants.urlReviewUpdate);
AppConstants.reviewInfoObj = constructReviewData();
return null;
}
private JSONObject constructReviewData() {
try {
JSONObject obj = AppConstants.reviewInfoObj;
JSONArray arr = obj.getJSONArray("userReviews");
//
obj = new JSONObject();
obj.put("uRating", ratedValue);
obj.put("uLike", "yes");
obj.put("uDisLike", "no");
obj.put("uPlace", address);
obj.put("uDateTime", currentDateandTime);
obj.put("uReview", reviewText);
arr.put(obj);
obj.put("userReviews", arr);
} catch (JSONException e) {
e.printStackTrace();
}
return obj;
}
}
首先点击其工作正常,当我在评论编辑文本中输入新文本时,我正在强行关闭
例外是:
android.view.WindowLeaked: Activity com.appdest.labelleza.ReviewActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView{426e2cb0 V.E..... R......D 0,0-350,250} that was originally added here
06-01 18:53:42.262: E/WindowManager(4142): at android.view.ViewRootImpl.<init>(ViewRootImpl.java:388)
06-01 18:53:42.262: E/WindowManager(4142): at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:256)
06-01 18:53:42.262: E/WindowManager(4142): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:94)
06-01 18:53:42.262: E/WindowManager(4142): at android.app.Dialog.show(Dialog.java:291)
06-01 18:53:42.262: E/WindowManager(4142): at com.appdest.labelleza.ReviewActivity$ReviewUpdateTask.onPreExecute(ReviewActivity.java:245)
06-01 18:53:42.262: E/WindowManager(4142): at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:587)
06-01 18:53:42.262: E/WindowManager(4142): at android.os.AsyncTask.execute(AsyncTask.java:535)
06-01 18:53:42.262: E/WindowManager(4142): at com.appdest.labelleza.ReviewActivity.onClick(ReviewActivity.java:207)
06-01 18:53:42.262: E/WindowManager(4142): at android.view.View.performClick(View.java:4470)
06-01 18:53:42.262: E/WindowManager(4142): at android.view.View$PerformClick.run(View.java:18599)
06-01 18:53:42.262: E/WindowManager(4142): at android.os.Handler.handleCallback(Handler.java:733)
06-01 18:53:42.262: E/WindowManager(4142): at android.os.Handler.dispatchMessage(Handler.java:95)
06-01 18:53:42.262: E/WindowManager(4142): at android.os.Looper.loop(Looper.java:157)
06-01 18:53:42.262: E/WindowManager(4142): at android.app.ActivityThread.main(ActivityThread.java:5633)
06-01 18:53:42.262: E/WindowManager(4142): at java.lang.reflect.Method.invokeNative(Native Method)
06-01 18:53:42.262: E/WindowManager(4142): at java.lang.reflect.Method.invoke(Method.java:515)
06-01 18:53:42.262: E/WindowManager(4142): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:896)
06-01 18:53:42.262: E/WindowManager(4142): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:712)
06-01 18:53:42.262: E/WindowManager(4142): at dalvik.system.NativeStart.main(Native Method)
任何人都建议我如何解决这个问题..?
答案 0 :(得分:0)
当您在onPreExecute()
方法中显示对话框时,AsyncTask会保留对您的活动的引用。这就是你获得内存泄漏的方式,这就是你得到Exception的原因。一种解决方案是使您的对话类成为您的活动的成员,并在onPause()
期间将其解除。
答案 1 :(得分:0)
确保在活动的onPause上将对话框设置为null,并在尝试调用其上的方法之前检查其是否为onOostExecute。