0

I've been simplifying and simplifying in an effort to get a string of text from an EditText in an AlertDialog without success

private void showAddSectorDlg() {
   if (BuildConfig.DEBUG) {
           Log.i(Constants.TAG_ACTSECTORS, "showAddSectorDialog() called.");
   }
   LayoutInflater inflater = getLayoutInflater();
   new AlertDialog.Builder(ActSectors.this)
   .setTitle(R.string.menu_new_sector)
   .setMessage(R.string.dlg_sect_add_msg)
   .setView(inflater.inflate(R.layout.dlg_sector_add, null))
   .setPositiveButton(R.string.dlg_save,
           new DialogInterface.OnClickListener() {
               public void onClick(
                       DialogInterface dialog,
                       int whichButton) {
                    EditText mNewSectorName = (EditText) findViewById(R.id.dlg_edt_nsector);   
                            //NullPointerException happens here:
                    String val = mNewSectorName.getText().toString();
                       if (BuildConfig.DEBUG) {
                        Log.i(Constants.TAG_ACTSECTORS, "New Sector Name: "+val);
                       }
                       grabVal(val);
                       dialog.dismiss();
                   }
           })
   .setNegativeButton(R.string.dlg_cancel,
           new DialogInterface.OnClickListener() {
               public void onClick(
                       DialogInterface dialog,
                       int whichButton) {
               }
           }).create().show();
   }

   private void grabVal(String newSector){
   mNSN = newSector;
   callCtlr(Constants.R_DB_ADDSECT);
   }

I would be grateful if anyone can explain where my error is...thanks!

Quasaur
  • 1,335
  • 1
  • 10
  • 27

1 Answers1

1

change your code as to get value from EditText from AlertDialog :

 LayoutInflater inflater = getLayoutInflater();
  View view=inflater.inflate(R.layout.dlg_sector_add,null);
   new AlertDialog.Builder(ActSectors.this)
   .setTitle(R.string.menu_new_sector)
   .setMessage(R.string.dlg_sect_add_msg)
   .setView(view)
   //... your code here

and now use view to access EditText from AlertDialog :

EditText mNewSectorName = (EditText)view.findViewById(R.id.dlg_edt_nsector);
ρяσѕρєя K
  • 132,198
  • 53
  • 198
  • 213
  • Thanks...that did it...i had to declare final View, but now it's returning the correct value! – Quasaur Feb 02 '13 at 16:57
  • @Quasaur : most welcome friend. and no need to declare view as final just declare it as class level field before onCreate method of Activity – ρяσѕρєя K Feb 02 '13 at 16:59