简体   繁体   中英

Android custom dialog with button

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

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); .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM