BArtWell
BArtWell

Reputation: 4044

AlertDialog with EditText in method: The local variable editText may not have been initialized

In my app I am using AlertDialog with EditText. I want to move this code to method because it called few times. I try to implement it by this way:

private EditText showEditAlert(DialogInterface.OnClickListener listener) {
    AlertDialog.Builder alert = new AlertDialog.Builder(this);
    alert.setTitle(R.string.waypoint);
    alert.setMessage(R.string.waypoint_alert_text);
    EditText editText = new EditText(this);
    alert.setView(editText);
    alert.setPositiveButton(android.R.string.ok, listener);
    alert.setNegativeButton(android.R.string.cancel, null);
    alert.show();
    return editText;
}

And then I want to use it:

final EditText editText = showEditAlert(new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int whichButton) {
        // Here is I am working with editText
        // and here is I get error "The local variable editText may not have been initialized"
    }
});

But I get "The local variable editText may not have been initialized" error. As I understand compiler think that the onClick() method will called before showEditAlert() returns value.

How to implement this correctly? Or may be I can just suppress this error?

Upvotes: 0

Views: 701

Answers (2)

Phuong
Phuong

Reputation: 1373

Because the editText variable may not be initialized. The IDE could not check it when it is creating.

The solution here is use the keyword: "this"

final EditText editText = showEditAlert(new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int whichButton) {
        this.getText();// this mean editText, not its parent, if you want to use parent, you must have ParentClass.this
    }
});

Upvotes: 0

matiash
matiash

Reputation: 55340

Looks like the IDE's warning is precisely for the reason you suppose. I guess you could circumvent this by defining a custom interface for the listener (and it would probably be more clear to use, to boot). Something like:

interface EditAlertOkListener
{
    void onEditAlertOk(EditText editText);
}

private void showEditAlert(final EditAlertOkListener listener)
{
    AlertDialog.Builder alert = new AlertDialog.Builder(this);
    ...
    final EditText editText = new EditText(this);
    alert.setView(editText);
    alert.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener()
    {
        @Override
        public void onClick(DialogInterface dialog, int which)
        {
            if (listener != null)
                listener.onEditAlertOk(editText);
        }
    }); 

    alert.setNegativeButton(android.R.string.cancel, null);
    alert.show();
}

And then just call it with:

    showEditAlert(new EditAlertOkListener() {
        @Override
        public void onEditAlertOk(EditText editText) {
            // Use editText here.
        }
    });     

PS. You can also have the method return the EditText, if needed, I just removed it to make this example clearer. Or also, if you just need the EditText contents, have the interface pass a CharSequence or String instead of the EditText itself. This is just a template. :)

Upvotes: 1

Related Questions