Yves
Yves

Reputation: 25

Android Dialog EditText doesn't give value

I'm trying to get the text from a Dialog, but it doesn't work. All the code works except username = txtDialog.getText().toString();

I get a NullpointerException

Here is the complete code :

btn_next.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            dialog  = new Dialog(ResActivity.this);
            dialog.setContentView(R.layout.custom);
            dialog.show();

            Button declineButton = (Button) dialog.findViewById(R.id.declineButton);
            declineButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    txtDialog = (EditText)findViewById(R.id.textDialog);
                    username = txtDialog.getText().toString();
                    Intent intent = new Intent(getApplicationContext(), EndActivity.class);
                    intent.putExtra(MESSAGE_NAME, username);
                    startActivity(intent);
                    dialog.dismiss();
                }
            });

        }
    });

Can someone help ?

Upvotes: 0

Views: 90

Answers (5)

Don Chakkappan
Don Chakkappan

Reputation: 7560

void connectToHotSpot() { AlertDialog.Builder alert = new AlertDialog.Builder(this);

        alert.setTitle("Inpaint Server");

        alert.setMessage("IP Address");

        final EditText input = new EditText(this);
        alert.setView(input);


        alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int whichButton) {

            String nickname=input.getText().toString();
            if(!nickname.equals(""))
            {
                Variables.Serversip=nickname;
                ipaddress.setText("Server's IP "+nickname);
                showAlertMessage("Message","Ok.Proceed");
            }
            else
            {
                showAlertMessage("Oops","Specify the Server's IP Address");
            }

         }
        });

        alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
         public void onClick(DialogInterface dialog, int whichButton) {
            Toast.makeText(getApplicationContext(),"Must give me the IP B4 Inpainting",Toast.LENGTH_LONG).show();   
        }
        });

         alert.show();
    }

Upvotes: 0

Lokesh
Lokesh

Reputation: 5368

I think your EditText inside of your dialog:

then change this declaration line

From

txtDialog = (EditText)findViewById(R.id.textDialog);

To

txtDialog = (EditText)dialog.findViewById(R.id.textDialog);

Upvotes: 0

Bhanu Sharma
Bhanu Sharma

Reputation: 5145

your edit text inside in dialog i.e use like this

txtDialog = (EditText)dialog.findViewById(R.id.textDialog);

Upvotes: 0

kalyan pvs
kalyan pvs

Reputation: 14590

I think your Edittext inside Dialog Change this line

txtDialog = (EditText)findViewById(R.id.textDialog);

into

txtDialog = (EditText)dialog.findViewById(R.id.textDialog);

Upvotes: 2

Nand
Nand

Reputation: 614

try this

txtDialog = (EditText)dialog.findViewById(R.id.textDialog);

Upvotes: 2

Related Questions