我正在片段中实现可扩展列表视图,我正在为该集合创建一个 groupcollection 和组列表,面临 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)