9

I have the following code that create alert dialog and I added two edit text to it but once I run the app the values within the EditText won't be retrived and my app crash with NullPointerException:

the code is:

AlertDialog.Builder alert = new AlertDialog.Builder(this);
        LayoutInflater inflater=this.getLayoutInflater();
        final EditText usernameInput=(EditText)findViewById(R.id.dialogusername);
        final EditText passwordInput=(EditText)findViewById(R.id.dialogpassword);   
        alert.setView(inflater.inflate(R.layout.dialog,null));
        alert.setTitle("Enter Password");
        alert.setMessage("Enter password to remove the app:");
        alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int whichButton) {
            //provide user with caution before uninstalling
            //also here should be added a AsyncTask that going to read the password and once its checked the password is correct the app will be removed
            value1=usernameInput.getText().toString();
            value2=passwordInput.getText().toString();
            if(value1.equals(null)&&value2.equals(null))
            {Toast.makeText(context, "Enter username and password", Toast.LENGTH_SHORT).show();}
         }
        });
        });
        alert.show();
Sahil Mahajan Mj
  • 11,033
  • 8
  • 53
  • 100
Husam A. Al-ahmadi
  • 2,056
  • 2
  • 20
  • 27
  • You call wrong findViewById. Call alert.findViewByID – Yahor10 Nov 27 '12 at 12:17
  • thanks Yahor10, but I tried to do it your way and keep getting an error since there is no function called alert.findViewById(); the only thing that I got is this.findViewById() which also did not work.. but Any how I posted the solution that I used to solve this problem below.. – Husam A. Al-ahmadi Nov 28 '12 at 15:15

3 Answers3

25

Thanks guys for your contributions in answering my question, and I think I got the solution for the problem that I posted above which is:

AlertDialog.Builder alert = new AlertDialog.Builder(MyFeedActivity.this);
        LayoutInflater inflater=MyFeedActivity.this.getLayoutInflater();
        //this is what I did to added the layout to the alert dialog
        View layout=inflater.inflate(R.layout.dialog,null);       
        alert.setView(layout);
        final EditText usernameInput=(EditText)layout.findViewById(R.id.dialogusername);
        final EditText passwordInput=(EditText)layout.findViewById(R.id.dialogpassword);

and I think the problem was that I cannot get the EidtText within the alert dialog, but by doing it by the above code every thing works just fine with me.

Husam A. Al-ahmadi
  • 2,056
  • 2
  • 20
  • 27
14

use this:

final AlertDialog alertDialog = new AlertDialog.Builder(this).create();
    final EditText input = new EditText(this);
    input.setHint("hint");
    alertDialog.setTitle("title");
    alertDialog.setMessage(message);
    alertDialog.setView(input);
Jignesh Ansodariya
  • 12,583
  • 24
  • 81
  • 113
1

Try editing like this

final EditText usernameInput=(EditText)this.findViewById(R.id.dialogusername);
final EditText passwordInput=(EditText)this.findViewById(R.id.dialogpassword);

OR

final EditText usernameInput=(EditText)alert.findViewById(R.id.dialogusername);
final EditText passwordInput=(EditText)alert.findViewById(R.id.dialogpassword);
Shruti
  • 1
  • 13
  • 55
  • 95