tags:

views:

22

answers:

1

How does one access a particular widget from within a custom layout while using Alert Builder? As you can see below, I'm setting the alert to a widget that is created in the code, but I'd MUCH rather use predefined layout.

Current code:

    private AlertDialog numberDialog(String title, final int dialogID){
            AlertDialog.Builder alert = new AlertDialog.Builder(this);                 
            alert.setTitle(title);  

            final NumberPicker np = new NumberPicker(this);
            alert.setView(np);
            alert.setPositiveButton(R.string.done, new DialogInterface.OnClickListener() {  
                    public void onClick(DialogInterface dialog, int whichButton) {
                            int newNumber = np.getCurrent();
                            calculatorVO.setWaiste(newNumber);
                            PFACalculator.this.updateList();
                            return;                  
                    }  
            });  

            alert.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                            return;   
                    }
            });

            AlertDialog ad = alert.create();
            return ad;
    }

Psuedo-code for something I'd like:

    private AlertDialog numberDialog(String title, final int dialogID){
            AlertDialog.Builder alert = new AlertDialog.Builder(this);                 
            alert.setTitle(title);  
            alert.setView(R.layout.numberDialog);
            alert.setPositiveButton(R.string.done, new DialogInterface.OnClickListener() {  
                    public void onClick(DialogInterface dialog, int whichButton) {
                            NumberPicker np = (NumberPicker)findViewById(R.id.numpicker);
                            int newNumber = np.getCurrent();
                            calculatorVO.setWaiste(newNumber);
                            PFACalculator.this.updateList();
                            return;                  
                    }  
            });  

            alert.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                            return;   
                    }
            });

            AlertDialog ad = alert.create();
            return ad;
    }

However, whenever I try to do it the second way I get a null pointer exception. Can anyone help?

+1  A: 

The view you are assigning hasn't been created yet. Take a look at LayoutInfator.inflate() to inflate that view and pass the result to setView().

Mike
@Mike: Could you possible point me in the direction of a nice example?
Fran Fitzpatrick
Ah, nevermind. The dev docs are always a good place to start: http://developer.android.com/guide/topics/ui/dialogs.html
Fran Fitzpatrick