-3

I've got two customs AlertDialog. All work since I can't get the input of the user, I've created a class called MyDialog and put the constructor. Furthermore, I thought to put a getter with the EditText; nevertheless, I can't find the way to get the input.

Here is MyDialog class:

public class MyDialog {
    public static AlertDialog.Builder create(final Context context, final LayoutInflater layoutInflater, final String title, final String content) {
        View view = layoutInflater.inflate(R.layout.userdialog, null);
        ((TextView)view.findViewById(R.id.textViewTitleDialog)).setText(title);
        ((TextView)view.findViewById(R.id.textViewContentDialog)).setText(content);
        ((EditText)view.findViewById(R.id.etEmailNuevo)).getText();
        return new AlertDialog.Builder(context).setView(view);
    }

    public String getEmailNuevo(String NewEmail) {
        return NewEmail;
    }
}

I've created inside the EditText and instead of setText(), I put getText(); however, I'm using this class on my Fragment at the time. I don't know how to call it. I tried with getText().toString(); inside a Toast; nevertheless, it didn't show up. What I've done wrong?

Here is where I've used this class:

AlertDialog.Builder myDialog = MyDialog.create(context, getLayoutInflater(savedInstanceState), "Cambio de email", "Introduce nuevo email");
myDialog.setPositiveButton(R.string.canviaruserdialog, new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int id) {

    }
})
.setNegativeButton(R.string.cancelaruserdialog, null)
.show();

EDIT 1

I've changed my code to:

final AlertDialog.Builder myDialog = MyDialog.create(context, getLayoutInflater(savedInstanceState), "Cambio de email", "Introduce nuevo email");
final String pew = MyDialog.getEntry();
myDialog.setPositiveButton(R.string.canviaruserdialog, new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int id) {
        Toast.makeText(getActivity(), pew, Toast.LENGTH_LONG).show();
    }
})
.setNegativeButton(R.string.cancelaruserdialog, null)
.show();

And my class looks like:

private static EditText mEditText;
public static AlertDialog.Builder create(final Context context, final LayoutInflater layoutInflater, final String title, final String content) {
    View view = layoutInflater.inflate(R.layout.userdialog, null);
    ((TextView)view.findViewById(R.id.textViewTitleDialog)).setText(title);
    ((TextView)view.findViewById(R.id.textViewContentDialog)).setText(content);
    ((EditText)view.findViewById(R.id.etEmailNuevo)).getText();
    return new AlertDialog.Builder(context).setView(view);
}

public static String getEntry() {
    return mEditText.getText().toString();
}

And this is the Logcat error:

2927-2927/info.androidhive.slidingmenu E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: info.androidhive.slidingmenu, PID: 2927
java.lang.NullPointerException: Attempt to invoke virtual method 'android.text.Editable android.widget.EditText.getText()' on a null object reference
    at info.androidhive.slidingmenu.MyDialog.getEntry(MyDialog.java:26)
    at info.androidhive.slidingmenu.ConfiguracionFragment.onClick(ConfiguracionFragment.java:45)
    at android.view.View.performClick(View.java:4756)
    at android.view.View$PerformClick.run(View.java:19749)
    at android.os.Handler.handleCallback(Handler.java:739)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:135)
    at android.app.ActivityThread.main(ActivityThread.java:5221)
    at java.lang.reflect.Method.invoke(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:372)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)

Edit 2

I've changed the logic and now, I'm trying to do this way:

I've created an EditText and a String as follows:

EditText mEt;
private String m_Text = "";

Here is where I initiate this EditText:

mEt = (EditText) rootView.findViewById(R.id.etEmailNuevo);

Then, I do the AlertDialog as follows:

LayoutInflater inflater = getLayoutInflater(savedInstanceState);
View dialoglayout = inflater.inflate(R.layout.userdialog, null);
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());

Dialog dialog = new Dialog(getActivity());
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);

final EditText input = mEt; //null<--------

input.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
builder.setView(dialoglayout);

builder.setPositiveButton("Cambiar", new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialog, int which) {
        m_Text = input.getText().toString();
        Toast.makeText(getActivity(), m_Text, Toast.LENGTH_LONG).show();
    }
});
builder.setNegativeButton("Cancelar", new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialog, int which) {
        dialog.cancel();
    }
});

builder.show();

However, it is still giving me this EditText is null. I don't know how to link this EditText with R.id.etEmailNuevo from userdialog.

MRS1367
  • 1,053
  • 1
  • 14
  • 40
Skizo-ozᴉʞS ツ
  • 19,464
  • 18
  • 81
  • 148

1 Answers1

1

You're not using getText() correctly.

If you want to get the content of an EditText in your dialog, you should do the following:

1) In the MyDialog class, create a member variable for your EditText

private EditText mEditText;

Set the findViewById(R.id.etEmailNuevo) line to equal that.

2) Create a method which gets the current content of the EditText

public String getEntry() {
     return mEditText.getText().toString();
}

3) Then when you create your dialog, in onClick call this method and you're all set.

JMRboosties
  • 15,500
  • 20
  • 76
  • 116