teekib
teekib

Reputation: 2821

Android:Nullpointerexception when using expandablelistview in fragment

I am implementing expandablelistview in a fragment, i am creating a groupcollection and group list for that collection, facing nullpointerexception, not sure where i am doing wrong

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
          View view = inflater.inflate(R.layout.first_tab_layout, null);


        createGroupList();

        createCollection();

        expListView = (ExpandableListView) getActivity().findViewById(R.id.laptop_list);
        final ExpandableListAdapter expListAdapter = new ExpandableListAdapter(
                getActivity(), groupList, laptopCollection);
        expListView.setAdapter(expListAdapter);

        expListView.setOnChildClickListener(new OnChildClickListener() {

            public boolean onChildClick(ExpandableListView parent, View v,
                    int groupPosition, int childPosition, long id) {
                final String selected = (String) expListAdapter.getChild(
                        groupPosition, childPosition);
                Toast.makeText(getActivity(), selected, Toast.LENGTH_LONG)
                        .show();

                return true;
            }
        });

        return view;

    }

    private void createCollection() {
         groupList = new ArrayList<String>();
         groupList.add("HP");
         groupList.add("Dell");
         groupList.add("Lenovo");
         groupList.add("Sony");
         groupList.add("HCL");
         groupList.add("Samsung");

    }

    private void createGroupList() {
         // preparing laptops collection(child)
        String[] hpModels = { "HP Pavilion G6-2014TX", "ProBook HP 4540",
                "HP Envy 4-1025TX" };
        String[] hclModels = { "HCL S2101", "HCL L2102", "HCL V2002" };
        String[] lenovoModels = { "IdeaPad Z Series", "Essential G Series",
                "ThinkPad X Series", "Ideapad Z Series" };
        String[] sonyModels = { "VAIO E Series", "VAIO Z Series",
                "VAIO S Series", "VAIO YB Series" };
        String[] dellModels = { "Inspiron", "Vostro", "XPS" };
        String[] samsungModels = { "NP Series", "Series 5", "SF Series" };

        laptopCollection = new LinkedHashMap<String, List<String>>();

        for (String laptop : groupList) {
            if (laptop.equals("HP")) {
                loadChild(hpModels);
            } else if (laptop.equals("Dell"))
                loadChild(dellModels);
            else if (laptop.equals("Sony"))
                loadChild(sonyModels);
            else if (laptop.equals("HCL"))
                loadChild(hclModels);
            else if (laptop.equals("Samsung"))
                loadChild(samsungModels);
            else
                loadChild(lenovoModels);

            laptopCollection.put(laptop, childList);
        }
    }

    private void loadChild(String[] laptopModels) {
         childList = new ArrayList<String>();
            for (String model : laptopModels)
                childList.add(model);
    }

Getting exception at line 104: that is for (String laptop : groupList) {

below is my trace

08-27 12:35:28.732: E/AndroidRuntime(22658):    at com.me.myapp.SecondFragment.createGroupList(SecondFragment.java:104)
08-27 12:35:28.732: E/AndroidRuntime(22658):    at com.me.myapp.SecondFragment.onCreateView(SecondFragment.java:53)
08-27 12:35:28.732: E/AndroidRuntime(22658):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:870)
08-27 12:35:28.732: E/AndroidRuntime(22658):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1080)
08-27 12:35:28.732: E/AndroidRuntime(22658):    at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:622)

Upvotes: 0

Views: 722

Answers (2)

Piyush
Piyush

Reputation: 18923

This is because you are call your function before your id set for ExpandableListView So you have to find it before your functions called.

and also change

expListView = (ExpandableListView) getActivity().findViewById(R.id.laptop_list);

to

expListView = (ExpandableListView) view.findViewById(R.id.laptop_list);

Upvotes: 1

Tarek Kanon
Tarek Kanon

Reputation: 98

you are trying to call an expandable list which is not exist in the main activity here

expListView = (ExpandableListView) getActivity().findViewById(R.id.laptop_list);

instead you need to call it from the correct view try this

expListView = (ExpandableListView) view.findViewById(R.id.laptop_list);

Upvotes: 1

Related Questions