2010-09-07 1 views
0

Comment accéder à un widget particulier depuis une mise en page personnalisée en utilisant Alert Builder? Comme vous pouvez le voir ci-dessous, je mets l'alerte à un widget qui est créé dans le code, mais j'utiliserais BEAUCOUP plutôt la disposition prédéfinie.Comment accéder à un widget depuis une mise en page avec Alert Builder?

Code actuel:

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 pour quelque chose que je voudrais:

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; 
    } 

Cependant, chaque fois que je tente de le faire la deuxième façon, je reçois une exception de pointeur nul. Quelqu'un peut-il aider?

Répondre

1

La vue que vous attribuez n'a pas encore été créée. Jetez un oeil à LayoutInfator.inflate() pour gonfler cette vue et passez le résultat à setView().

+0

Pourriez-vous me diriger dans la direction d'un bel exemple? –

+0

Ah, rien à faire. Les dev docs sont toujours un bon point de départ: http://developer.android.com/guide/topics/ui/dialogs.html –

Questions connexes