0

运行代码时出现以下错误。我不知道如何阻止内存泄漏并摆脱这个问题。

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();
    } 

请指导我解决此问题的最佳方法

4

4 回答 4

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();

在完成您的活动之前。

于 2013-08-30T05:35:42.687 回答
2

您已经显示了一个对话框。完成活动后,在完成活动之前关闭它。由于您还没有关闭它,所以窗口错误即将到来。

像这样的事情:

@Override
public void OnDestory() {
    if (dialog != null) {
        dialog.dismiss();
        dialog = null;
    }
}
于 2013-08-30T05:27:22.227 回答
2

Android 建议不要使用 Thread,而是使用AsyncTask,它还可以处理 UI Thread 中的响应,而 Thread 无法做到这一点。

我使用 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();
于 2013-08-30T05:44:29.517 回答
1

这是因为您试图在后台线程中调用 finish()。

将 finish() 放入 runOnUIThread() 方法中。

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();

                        }
                    });
于 2013-08-30T05:26:40.247 回答