2013-08-27 75 views
0

我在一個片段執行expandablelistview,我創建該集合的groupcollection和組列表,面對NullPointerException異常,不知道在哪裏我做錯安卓:當片段使用expandablelistview

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

獲取異常NullPointerException異常在管線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) 

回答

1

這是因爲你是您的ID集之前調用函數ExpandableListView 所以你必須在你的函數調用之前找到它。

,改變

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

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

你想叫擴張這是不是在這裏的主要活動存在列表

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

,而不是你需要從正確的觀點稱之爲試試這個

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