Search code examples
androidlistviewfragmentandroid-listfragmentonitemclicklistener

Inflate a ListView inside a OnItemSelectedListener that is inside a Fragment - java.lang.Null Error in setAdapter


I'm going crazy, i get this error: at this line listView.setAdapter(dataClassificationAdapter); :

   10-07 08:03:24.592: E/AndroidRuntime(2396): FATAL EXCEPTION: main
   10-07 08:03:24.592: E/AndroidRuntime(2396): Process: com.tfg.webquest, PID: 2396
   10-07 08:03:24.592: E/AndroidRuntime(2396): java.lang.NullPointerException
   10-07 08:03:24.592: E/AndroidRuntime(2396):  at com.tfg.webquest.HomeActivity$ClassificationTab.onCreateView(HomeActivity.java:698)

and more...

and I have this:

public class HomeActivity extends ActionBarActivity implements ActionBar.TabListener, OnItemSelectedListener {

public static class ClassificationTab extends Fragment{

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {

        View rootView = inflater.inflate(R.layout.fragment_home_classification, container,false);

        //Get the subject spinner
        spinnerSubjectClassification = (Spinner) rootView.findViewById(R.id.home_classification_spinner);
        //subjectSpinnerArray will show "(Choose a subject)"+all the subjects 
       String[] subjectSpinnerArray = new String[ss.length+1];
        //Insert in the subjectSpinnerArray "(Choose a subject)"
        subjectSpinnerArray[0]=getString(R.string.home_select_subject_string);
        //Insert in the subjectSpinnerArray all the subjects    
        for(int i=0;i<ss.length;i++){
            subjectSpinnerArray[i+1]=ss[i];
        }
        //Insert the information of the array on a list so as to introduce it in the adapter
        List<String> list = new ArrayList<String>();
             for(int i=0;i<subjectSpinnerArray.length;i++){
                    list.add(subjectSpinnerArray[i]);
              }
                ArrayAdapter<String> dataAdapter2 = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_spinner_item,list);
                //Dropdown the adapter
                dataAdapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                //Show the adapter
                spinnerSubjectClassification.setAdapter(dataAdapter2);    
             //It activates the retrocalled method, called when we choose a subject
                spinnerSubjectClassification.setOnItemSelectedListener(mySubjectClassificationListener);
    }


    private OnItemSelectedListener mySubjectClassificationListener = new Spinner.OnItemSelectedListener() {

        public void onItemSelected(AdapterView<?> parent, View view, int pos,long id) {
            //Fill the subjectList
            for(int i=0;i<classification[pos-1].length;i++){
                ClassificationModel classificationModel = new ClassificationModel(classification[pos-1][i]);
                classificationList.add(classificationModel);
            }


            //create an ArrayAdaptar from the String Array
            dataClassificationAdapter = new MyCustomAdapter(ctx,R.layout.classification,classificationList);

            ListView listView = (ListView) getView().findViewById(R.id.classification_list);

            ArrayAdapter<String> stringAdapter = new ArrayAdapter<String>(getActivity(), 
                    android.R.layout.simple_list_item_2, classification[pos-1]);

            ListView listView = (ListView) getView().findViewById(R.id.listview_classification); 

            // Assign adapter to ListView

            listView.setAdapter(dataClassificationAdapter);
        }
    }

}

I need to inflate a listView inside a onClickListener because the content of the listview depends on the spinner selected item . Thank you for your time.


Solution

  • Replace getView().findViewById() with getActivity().findViewById().

    But you should consider moving your ListView creation to onCreateView (then use rootView.findViewById()) and in OnItemSelected only change data inside your adapter