How to get the value of views that have been "inflated"?

0

Good, I have a problem and I have been researching and I have not found a solution, the problem is the following, wanting to get the value of an edittext that I have added dynamically stops my application and shows me the following error

java.lang.NullPointerException: Attempt to invoke virtual method 'android.text.Editable android.widget.EditText.getText()' on a null object reference

and the following code is where it is used to appear the views

 private void agregarSpinner(View view){        
    View infla = View.inflate(getActivity(),R.layout.nuevos_spinner_materiales,layout);        
    Spinner spinnerMateriales2 = (Spinner) view.findViewById(R.id.spinner2);
    spinnerMateriales2.setAdapter(adapter);      

}

and when reading the error I realize the problem, what happens that I do not know how to initialize the views that I add to obtain its value

    
asked by JesusSh 04.01.2018 в 01:18
source

1 answer

1

If you inflate a component dynamically, you have to use the parent component that you inflated to bring the childs reference.

For example, if you made the inflate as follows:

 View infla = View.inflate(getActivity(),R.layout.nuevos_spinner_materiales,layout);

And you try to get the child component reference in the following way:

 private void agregarSpinner(View view){               
    Spinner spinnerMateriales2 = (Spinner) view.findViewById(R.id.spinner2);
}

You could get an error, if spinner2, was inside "new_spinner_materials", what you should do would be as follows:

 private void agregarSpinner(View view){      
 View infla = View.inflate(getActivity(),R.layout.nuevos_spinner_materiales,layout);            
    Spinner spinnerMateriales2 = (Spinner) infla.findViewById(R.id.spinner2);
}
    
answered by 04.01.2018 / 13:52
source