0

I got the below error while running my code. I don't have idea how to stop the leaking memory and get rid off this issue.

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)

I am using the below code where i am getting the Memory Leak issue. when i execute this method that issue occurred

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

Please guide me the best way to solve this issue

Helping_Hand
  • 203
  • 1
  • 5
  • 10

4 Answers4

3

I think its because context you used in your progress dialog. Try getBaseContext() or getApplicationContext() in your progress dialog initialization.

Instead of

 dialog = ProgressDialog.show(PaymentActivity.this, "", "Processing Transaction...", true);

Try

dialog = ProgressDialog.show(getApplicationContext(), "", "Processing Transaction...", true);

Or

 dialog = ProgressDialog.show(getBaseContext(), "", "Processing Transaction...", true);

And also do

dialog.dismiss();

before finishing your activity.

Vipul Purohit
  • 9,677
  • 6
  • 50
  • 76
  • Didn't worked with getApplicationContext() and getBaseContext() giving below error message in logcat 08-30 11:26:37.982: E/AndroidRuntime(317): android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application 08-30 11:26:37.982: E/AndroidRuntime(317): at android.view.ViewRoot.setView(ViewRoot.java:509) 08-30 11:26:37.982: E/AndroidRuntime(317): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:177) 08-30 11:26:37.982: E/AndroidRuntime(317): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91) – Helping_Hand Aug 30 '13 at 05:59
  • 2
    try dismissing progress dialog before finishing activity. – Vipul Purohit Aug 30 '13 at 06:02
  • Yes i did and seems working fine. Now doing some more testing – Helping_Hand Aug 30 '13 at 06:13
2

You have shown a dialog. Close it before finishing the activity once your are done with it. Since you have not closed it, so window error is coming.

Soething like this:

@Override
public void OnDestory() {
    if (dialog != null) {
        dialog.dismiss();
        dialog = null;
    }
}
Sushil
  • 7,711
  • 2
  • 31
  • 61
2

Instead of using Thread, Android has adviced to use AsyncTask which also handle response in UI Thread which Thread can't do.

I modified your code using AsyncTask. Try to implement it.

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"
    }
}

To use this,

new UpdateInfoAsyncTask(YourActivity.this,10,"hi").execute();
Chintan Rathod
  • 24,674
  • 13
  • 76
  • 92
1

This is because you are trying to call finish() in background thread.

put finish() in runOnUIThread() method.

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

                        }
                    });
Biraj Zalavadia
  • 27,124
  • 9
  • 56
  • 73