运行我的代码时遇到以下错误。我不知道如何阻止泄漏记忆并摆脱这个问题。
08-30 10:00:32.538: E/WindowManager(851): Activity simplicity.in.TenderPopUpTabsActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@43fce660 that was originally added here
08-30 10:00:32.538: E/WindowManager(851): android.view.WindowLeaked: Activity simplicity.in.TenderPopUpTabsActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@43fce660 that was originally added here
08-30 10:00:32.538: E/WindowManager(851): at android.view.ViewRoot.<init>(ViewRoot.java:247)
08-30 10:00:32.538: E/WindowManager(851): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:148)
08-30 10:00:32.538: E/WindowManager(851): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
08-30 10:00:32.538: E/WindowManager(851): at android.view.Window$LocalWindowManager.addView(Window.java:424)
08-30 10:00:32.538: E/WindowManager(851): at android.app.Dialog.show(Dialog.java:241)
08-30 10:00:32.538: E/WindowManager(851): at android.app.ProgressDialog.show(ProgressDialog.java:107)
08-30 10:00:32.538: E/WindowManager(851): at android.app.ProgressDialog.show(ProgressDialog.java:90)
08-30 10:00:32.538: E/WindowManager(851): at simplicity.in.PaymentActivity.transByPaymentGateway(PaymentActivity.java:263)
08-30 10:00:32.538: E/WindowManager(851): at simplicity.in.PaymentActivity.onClick(PaymentActivity.java:131)
08-30 10:00:32.538: E/WindowManager(851): at android.view.View.performClick(View.java:2408)
08-30 10:00:32.538: E/WindowManager(851): at android.view.View$PerformClick.run(View.java:8816)
08-30 10:00:32.538: E/WindowManager(851): at android.os.Handler.handleCallback(Handler.java:587)
08-30 10:00:32.538: E/WindowManager(851): at android.os.Handler.dispatchMessage(Handler.java:92)
08-30 10:00:32.538: E/WindowManager(851): at android.os.Looper.loop(Looper.java:123)
08-30 10:00:32.538: E/WindowManager(851): at android.app.ActivityThread.main(ActivityThread.java:4627)
08-30 10:00:32.538: E/WindowManager(851): at java.lang.reflect.Method.invokeNative(Native Method)
08-30 10:00:32.538: E/WindowManager(851): at java.lang.reflect.Method.invoke(Method.java:521)
08-30 10:00:32.538: E/WindowManager(851): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
08-30 10:00:32.538: E/WindowManager(851): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
08-30 10:00:32.538: E/WindowManager(851): at dalvik.system.NativeStart.main(Native Method)
我正在使用以下代码来解决内存泄漏问题。当我执行此方法时发生了问题
private void transByPaymentGateway(final double ccAmt) {
dialog = ProgressDialog.show(PaymentActivity.this, "", "Processing Transaction...", true);
new Thread(new Runnable() {
public void run() {
boolean successful = false;
final String totalAmt;
if(ccAmt <25){
totalAmt = Double.toString(amount);
AnyPayment anyPayment = new AnyPayment(PaymentActivity.this);
successful = anyPayment.payAmount(Name, Number,totalAmt);
}
}
if(successful){
runOnUiThread(new Runnable() {
public void run() {
showApprovalToast();
Variables.creditCardAmount = amount;
getReceiptfromPrinter();
saveTransactioDetails();
if(Variables.customerEmailId.equals("")){
System.out.println("Customer is Not associated");
}
else{
System.out.println("Testing email"+Variables.customerEmailId);
System.out.println("Testing"+Variables.customerId);
FacebookSharingUtils facebookSharingUtils = new FacebookSharingUtils(PaymentActivity.this);
facebookSharingUtils.execute();
}
}
});
finish();
}
else{
runOnUiThread(new Runnable() {
public void run() {
showCcSwipeMessage();
infoOnSwipeEditText.setText("");
dialog.dismiss();
}
});
}
}
}).start();
}
请指导我解决此问题的最佳方法
答案 0 :(得分:3)
我认为这是因为您在context
中使用了progress dialog
。在getBaseContext()
初始化中尝试getApplicationContext()
或progress dialog
。
而不是
dialog = ProgressDialog.show(PaymentActivity.this, "", "Processing Transaction...", true);
<强>尝试强>
dialog = ProgressDialog.show(getApplicationContext(), "", "Processing Transaction...", true);
或强>
dialog = ProgressDialog.show(getBaseContext(), "", "Processing Transaction...", true);
并且
dialog.dismiss();
在完成活动之前。
答案 1 :(得分:2)
您已显示对话框。完成活动后关闭它,一旦完成它。由于您尚未关闭它,因此窗口错误即将到来。
这样说:
@Override
public void OnDestory() {
if (dialog != null) {
dialog.dismiss();
dialog = null;
}
}
答案 2 :(得分:2)
Android建议使用 AsyncTask ,而不是使用Thread,它也处理线程无法做的UI线程中的响应。
我使用AsyncTask修改了你的代码。尝试实现它。
public class UpdateInfoAsyncTask extends AsyncTask<Void, Void, Boolean> {
int intValue;
String strValue;
ProgressDialog dialog;
Context context;
public UpdateInfoAsyncTask(Context context,int intValue, String strValue) {
this.context = context;
this.intValue = intValue;
this.strValue = strValue;
}
@Override
protected void onPreExecute() {
dialog = ProgressDialog.show(context, "Dialog Title",
"Dialog message");
}
@Override
protected Boolean doInBackground(Void... params) {
// use intValue
// use strValue
// do your web service call or other stuff
if(successful)
return true;
else
return false;
}
@Override
protected void onPostExecute(Boolean result) {
if (dialog != null)
dialog.dismiss();
// do some UI operation here which you have done in "runOnUiThread"
}
}
要使用它,
new UpdateInfoAsyncTask(YourActivity.this,10,"hi").execute();
答案 3 :(得分:1)
这是因为你试图在后台线程中调用finish()。
在runOnUIThread()方法中输入finish()。
runOnUiThread(new Runnable() {
public void run() {
showApprovalToast();
Variables.creditCardAmount = amount;
getReceiptfromPrinter();
saveTransactioDetails();
if(Variables.customerEmailId.equals("")){
System.out.println("Customer is Not associated");
}
else{
System.out.println("Testing email"+Variables.customerEmailId);
System.out.println("Testing"+Variables.customerId);
FacebookSharingUtils facebookSharingUtils = new FacebookSharingUtils(PaymentActivity.this);
facebookSharingUtils.execute();
}
finish();
}
});