Android:在片段中使用expandablelistview时的Nullpointerexception

时间:2013-08-27 07:13:33

标签: android nullpointerexception expandablelistview

我正在片段中实现expandablelistview,我正在为该集合创建一个groupcollection和group list,面对nullpointerexception,不确定我在哪里做错了

@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);
    }

在第104行获取例外:即for (String laptop : groupList) {

下面是我的追踪

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)

2 个答案:

答案 0 :(得分:1)

您正在尝试调用此主要活动中不存在的可扩展列表

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

而你需要从正确的视图中调用它来试试这个

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

答案 1 :(得分:1)

这是因为您在为 ExpandableListView 设置ID之前调用了您的函数 所以你必须在调用函数之前找到它。

并且还要改变

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

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