Click here to Skip to main content
15,887,135 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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:
Java
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:
Java
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:
Java
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:
Java
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:
Java
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. I've created an EditText and a String as follows:
Java
EditText mEt;
private String m_Text = "";

Here is where I initiate this EditText:
Java
mEt = (EditText) rootView.findViewById(R.id.etEmailNuevo);

Then, I do the AlertDialog as follows:
Java
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.
Posted
Updated 3-Jan-17 9:25am
v2

1 solution

What is rootView? According to your code logic, you should use dialoglayout instead of rootView.
Java
TextView mEt = (EditText)dialoglayout.findViewById(R.id.etEmailNuevo);

after inflating your layout.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900