Cuadro de diálogo personalizado de Android con botón

Very simple question today. When I attempt to retreive a reference to the button on the dialog I always receive a null value.

@Override
public void onRestart() {
    super.onRestart();


    //must prompt with modal dialog for pin
    Dialog dialog = new Dialog(this);
    dialog.setOwnerActivity(this);
    dialog.setCancelable(false);

    //check pin
    dialog.setCanceledOnTouchOutside(false);
    //set view to enterpin XML screen
    dialog.setContentView(R.layout.enterpin);
    //register button

    //show dialog
    dialog.show();
    //listen for button being clicked
    Button button = (Button) findViewById(R.id.pinlogin);
    button.setOnClickListener(new OnClickListener(){
        @Override
        public void onClick(View v){
            EditText editText = (EditText) findViewById(R.id.enterpin);
            int enteredPin = Integer.parseInt(editText.getText().toString());
            SharedPreferences sharedP = getPreferences(MODE_PRIVATE);
            int temp = sharedP.getInt("pin", 0);
            if(enteredPin==temp){
                pinCheck = true;
            }else{
                pinCheck = false;
            }
        }
    });
    if(pinCheck){
        dialog.dismiss();
    }



}

Yes the button exists, no I did not mispell it's reference. Yes I've cleaned the project and restarted eclipse. How can the button not be associated with the view when i clearly called setContentView()? I'm sure it's simple, I've never used dialogs before, new to GUI in general. Especially Android

preguntado el 12 de junio de 12 a las 14:06

1 Respuestas

Button button = (Button)dialog.findViewById(R.id.pinlogin);
                        ^^^^^^

When you show Dialog ,you have to give refrences of dialog.because without it ,Button refrences from main.xml.i mean from main view setContentView(R.layout.main);.

Respondido el 12 de junio de 12 a las 14:06

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.