質問

I have a menu button, that when I click it, it sends some data to the cloud. While it is sending the data, I display a progress dialog. Every thing goes swimmingly and seems to be fine and I can press the button as many times as I want and the data is properly sent to the cloud. But when I go to exit the activity I get a error saying that there was a window leak:

com.android.internal.policy.impl.PhoneWindow$DecorView{4321fd38 V.E..... R......D 0,0-1026,288} that was originally added here

the "here" is referring to when I insatiate my progress dialog.

Here is my code:

 @Override
public boolean onOptionsItemSelected(MenuItem item) {
progressDialog = ProgressDialog.show(CruiseDetailRollCallActivity.this, "", "Loading...", true);//set up & show progress dialog

switch(item.getItemId()){
case R.id.menu_roll_call_opt_in:
    //saveing something into Parse -- (a database online, check Parse.com if you want more info, but just treat this like I am saving something into the cloud)
    currentUser.put("somethingBoolean", false); 
    currentUser.saveInBackground(new SaveCallback(){

    @Override
    public void done(ParseException e) { //once data has been put into the cloud
        progressDialog.dismiss();//dismiss the dialog
        supportInvalidateOptionsMenu();//refreshes the options menu
    }
    });

    return true;

default:
    return super.onOptionsItemSelected(item);
}
}

I should mention that this is not crashing my application but merely displaying the error. I have no clue why it is happening, and I feel like I shouldn't just ignore it.

EDIT: found my mistake. It was failing because works was because I was hitting back on the action bar and the progress dialog would be created but never dismissed since it was only being dismissed inside the done code.

役に立ちましたか?

解決 2

It was failing because works was because I was hitting back on the action bar and the progress dialog would be created but never dismissed since it was only being dismissed inside the done code.

I moved

progressDialog = ProgressDialog.show(CruiseDetailRollCallActivity.this, "", "Loading...", true);//set up & show progress dialog

into

case R.id.menu_roll_call_opt_in:
//saveing something into Parse -- (a database online, check Parse.com if you want more info, but just treat this like I am saving something into the cloud)
currentUser.put("somethingBoolean", false); 
currentUser.saveInBackground(new SaveCallback(){

@Override
public void done(ParseException e) { //once data has been put into the cloud
    progressDialog.dismiss();//dismiss the dialog
    supportInvalidateOptionsMenu();//refreshes the options menu
}
});

return true;

so looks something like this

case R.id.menu_roll_call_opt_in:
progressDialog = ProgressDialog.show(CruiseDetailRollCallActivity.this, "", "Loading...", true);//set up & show progress dialog
//saveing something into Parse -- (a database online, check Parse.com if you want more info, but just treat this like I am saving something into the cloud)
currentUser.put("somethingBoolean", false); 
currentUser.saveInBackground(new SaveCallback(){

@Override
public void done(ParseException e) { //once data has been put into the cloud
    progressDialog.dismiss();//dismiss the dialog
    supportInvalidateOptionsMenu();//refreshes the options menu
}
});

return true;

他のヒント

My guess is that you're leaking the activity into the currentUser object, when you pass the CruiseDetailRollCallActivity into currentUser.saveInBackground(new SaveCallback().... The SaveCallback class that you just created now has a strong reference to the Activity. It will never be garbage collected, even though you exit the method. You should use a WeakReference and then it can be garbage collected.

WeakReference<CruiseDetailRollCallActivity> weakRef = new WeakReference<CruiseDetailRollCallActivity>(CruiseDetailRollCallActivity.this)

Then, pass the weakRef in to the ProgressDialog constructor:

progressDialog = ProgressDialog.show(weakRef.get(), "", "Loading...", true);

Whenever you're passing around the Context in Android, check to see if you need a WeakReference so it can be garbage collected. It's easy to leak the entire application.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top