1

今天很简单的问题。当我尝试检索对对话框上按钮的引用时,我总是收到一个空值。

@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();
    }



}

是的,按钮存在,不,我没有拼错它的参考。是的,我已经清理了项目并重新启动了 Eclipse。当我清楚地调用 setContentView() 时,按钮如何不与视图关联?我敢肯定这很简单,我以前从未使用过对话框,一般来说是 GUI 新手。尤其是安卓

4

1 回答 1

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

当你显示对话框时,你必须给予。refrences of dialog因为没有它,Button refrences from main.xml我的意思是从主视图setContentView(R.layout.main);

于 2012-06-12T13:05:39.817 回答