Farhan Munir
Farhan Munir

Reputation: 99

Dialog Box - Issue accessing Value inside Edit Text

I have an EditText inside a dialog box in which user will enter a new category. However when I try to access the value inside the EditText - i get nullpointer exception.

Execution Process.

There is a spinner for categories When user selects new Category from the spinner a Dialog Box is shown to the user.

Code for the Dialog is as follows:

   //check if spCategories == new category
   if( arg0.getSelectedItem().toString().equals("New Category")) {
   //Show dialog box for adding new category
     Dialog dialog = new Dialog(AddTransaction.this);
     dialog.setContentView(R.layout.newcategory);
     dialog.setTitle("New Category");
     dialog.setCancelable(true);
     dialog.show();

     Button btnAddCat = (Button) dialog.findViewById(R.id.btnAddCategory);
     final EditText etCategory = (EditText) findViewById(R.id.etNewCategory);

     btnAddCat.setOnClickListener(new View.OnClickListener() {

     @Override
     public void onClick(View arg0) {
       **Log.v("EXT", "Category type is : " + etCategory.getText());**
     } 
                        }
     });

I am getting NullPointer exception when at the bolded line

Any ideas. Layout for Dialog is defined in an XML file.

Upvotes: 0

Views: 548

Answers (1)

gwvatieri
gwvatieri

Reputation: 5183

The line:

final EditText etCategory = (EditText) findViewById(R.id.etNewCategory);

is wrong, you have to change it in:

final EditText etCategory = (EditText) dialog.findViewById(R.id.etNewCategory);

Without "dialog." your are searching that view inside the activity layout instead of the dialog.

Upvotes: 1

Related Questions