Hello, I have a custom widget that can display a pop-up dialog.  I
have a single report of a NPE happening, and looking through my code,
I don't know how that's possible unless there is a race condition
involved somehow.  Just wondering if anybody can point me to the
problem.

Here's my code (irrelevant stuff omitted):


public class CurrencyEditText extends LinearLayout
{
    Dialog  mDialog;   // If the dialog is NOT showing, this is NULL

    protected void onDetachedFromWindow()
    {
        super.onDetachedFromWindow();

        // Dismiss, if showing
        if (mDialog != null)
        {
            mDialog.dismiss();
            mDialog = null;
        }
    }

    private void showDialog()
    {
            Builder builder = new Builder(context);

            // Set dialog properties, omitted...

            builder.setPositiveButton(R.string.set, new
OnClickListener()
            {
                public void onClick(DialogInterface dialog, int
buttonId)
                {
                    // Dismiss
                    CurrencyEditText.this.mDialog.dismiss();
                    CurrencyEditText.this.mDialog = null;

                    // Send message here, omitted...
                }
            });

            builder.setNegativeButton(android.R.string.cancel, new
OnClickListener()
            {
                public void onClick(DialogInterface dialog, int
buttonId)
                {
                    // Dismiss -- NPE happens on next line as mDialog
is NULL, which it shouldn't be
                    CurrencyEditText.this.mDialog.dismiss();
                    CurrencyEditText.this.mDialog = null;

                    // Send message here, omitted...
                }
            });

            mDialog = builder.show();
    }
}


That's it -- mDialog is not touched anywhere else.  So either the
click listener is called after onDetachedFromWindow() or before
builder.show() can set mDialog.

Obviously, I could address the symptom by butting a NULL check around
the crashing line, but I would like to understand what's going on here
as I use the same pattern all over my app.

Thanks.

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to