Android SherlockListFragment中的ExpandableListView,带有扩展BaseExpandableListAdapter的自定义适配器
你好强> 我有这个背景 主要活动 -扩展SherlockFragmentActivity -使用TabsAdapter显示4个片段 我在其中一个选项卡上遇到问题,我需要: 零碎技能 -扩展SherlockListFragment -显示一些硬编码测试字符串的可展开列表视图 ================================================================================== 问题: 我在这一点上得到了一个NullPointerException,我似乎无法旋转: //在FragmentSkills.java中的onActivityCreated()中 expandList=(ExpandableListView)getActivity().findViewById(R.id.ExpList) ================================================================================== 我有点被困在这里,我不知道还能尝试什么。我找到了很多类似的例子,但它们都只展示了如何在正常活动中做到这一点。由于我缺乏经验,这可能是我看不到的小东西,所以请看一看,并尝试在这方面帮助我 我是Android新手,所以请耐心等待我。感谢您的帮助。非常感谢。这是相关代码。如果你需要看更多,请告诉我非常感谢强> fragment_skills.xmlAndroid SherlockListFragment中的ExpandableListView,带有扩展BaseExpandableListAdapter的自定义适配器,android,listview,actionbarsherlock,expandablelistview,Android,Listview,Actionbarsherlock,Expandablelistview,你好 我有这个背景 主要活动 -扩展SherlockFragmentActivity -使用TabsAdapter显示4个片段 我在其中一个选项卡上遇到问题,我需要: 零碎技能 -扩展SherlockListFragment -显示一些硬编码测试字符串的可展开列表视图 ================================================================================== 问题: 我在这一点上得到了一个NullPointerExcept
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ExpandableListView android:id="@+id/ExpList"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:groupIndicator="@null" />
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingTop="10dp"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:paddingBottom="10dp"
android:orientation="vertical" >
<TextView android:id="@+id/tvSkillTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textIsSelectable="false"
android:textSize="19sp"
android:textStyle="normal"
android:paddingBottom="4dp" />
</LinearLayout>
list_item_skill_groups.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView android:id="@+id/tvSkillGroup"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginRight="15dp"
android:textColor="@color/black"
android:textIsSelectable="false"
android:textSize="17sp" />
</LinearLayout>
list_item_skills.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ExpandableListView android:id="@+id/ExpList"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:groupIndicator="@null" />
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingTop="10dp"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:paddingBottom="10dp"
android:orientation="vertical" >
<TextView android:id="@+id/tvSkillTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textIsSelectable="false"
android:textSize="19sp"
android:textStyle="normal"
android:paddingBottom="4dp" />
</LinearLayout>
FragmentSkills.java
public class FragmentSkills extends SherlockListFragment {
private ExpandableListView expandList;
private ArrayList<ExpandableSkillsGroup> expListItems;
private SkillsAdapter skillsAdapter;
ArrayList<ExpandableSkillsChild> list1;
ArrayList<ExpandableSkillsChild> list2;
ArrayList<ExpandableSkillsChild> list3;
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
expandList = (ExpandableListView) getActivity().findViewById(R.id.ExpList);
expListItems = getListItems();
skillsAdapter = new SkillsAdapter(getActivity(), expListItems);
expandList.setAdapter(skillsAdapter);
}
private ArrayList<ExpandableSkillsGroup> getListItems() {
list1 = new ArrayList<ExpandableSkillsChild>();
list1.add(new ExpandableSkillsChild("Android", null));
list1.add(new ExpandableSkillsChild("Java", null));
list1.add(new ExpandableSkillsChild("Javascript", null));
ExpandableSkillsGroup group1 = new ExpandableSkillsGroup("Software Development");
group1.setItems(list1);
list2 = new ArrayList<ExpandableSkillsChild>();
list2.add(new ExpandableSkillsChild("MySQL", null));
list2.add(new ExpandableSkillsChild("PostgreSQL", null));
list2.add(new ExpandableSkillsChild("SQLite", null));
ExpandableSkillsGroup group2 = new ExpandableSkillsGroup("Databases");
group2.setItems(list2);
list3 = new ArrayList<ExpandableSkillsChild>();
list3.add(new ExpandableSkillsChild("MySQL", null));
list3.add(new ExpandableSkillsChild("PostgreSQL", null));
list3.add(new ExpandableSkillsChild("SQLite", null));
ExpandableSkillsGroup group3 = new ExpandableSkillsGroup("Operating Systems");
group3.setItems(list3);
ArrayList<ExpandableSkillsGroup> groupList = new ArrayList<ExpandableSkillsGroup>();
groupList.add(group1);
groupList.add(group2);
groupList.add(group3);
return groupList;
}
}
公共类碎片技能扩展SherlockListFragment{
私有可扩展列表视图可扩展列表;
私有ArrayList解释项;
私人技能接受者技能接受者;
ArrayList列表1;
ArrayList列表2;
ArrayList列表3;
@凌驾
已创建ActivityState上的公共无效(Bundle savedInstanceState){
super.onActivityCreated(savedInstanceState);
expandList=(ExpandableListView)getActivity().findViewById(R.id.ExpList);
expListItems=getListItems();
skillsAdapter=新的skillsAdapter(getActivity(),expListItems);
expandList.setAdapter(skillsAdapter);
}
私有ArrayList getListItems(){
list1=新的ArrayList();
列表1.add(新的ExpandableSkillsChild(“Android”,null));
add(新的ExpandableSkillsChild(“Java”,null));
添加(新的ExpandableSkillsChild(“Javascript”,null));
ExpandableSkillsGroup 1=新的ExpandableSkillsGroup(“软件开发”);
组1.集合项目(列表1);
list2=新的ArrayList();
列表2.add(新的ExpandableSkillsChild(“MySQL”,null));
列表2.add(新的ExpandableSkillsChild(“PostgreSQL”,null));
列表2.add(新的ExpandableSkillsChild(“SQLite”,null));
ExpandableSkillsGroup 2=新的ExpandableSkillsGroup(“数据库”);
组2.集合项目(列表2);
list3=新的ArrayList();
添加(新的ExpandableSkillsChild(“MySQL”,null));
添加(新的ExpandableSkillsChild(“PostgreSQL”,null));
列表3.add(新的ExpandableSkillsChild(“SQLite”,null));
ExpandableSkillsGroup 3=新的ExpandableSkillsGroup(“操作系统”);
组3.集合项目(列表3);
ArrayList groupList=新建ArrayList();
groupList.add(group1);
groupList.add(group2);
groupList.add(group3);
返回群组列表;
}
}
首先用碎片技能扩展SherlockFragment
替换碎片技能扩展SherlockFragment
,这里不需要SherlockListFragment
。现在将此方法添加到您的片段技能中
:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_skills, container, false);
}
另外,在您的public void onActivityCreated(Bundle savedInstanceState)
中,而不是
expandList = (ExpandableListView) getActivity().findViewById(R.id.ExpList);
这样做
expandList = (ExpandableListView) getView().findViewById(R.id.ExpList);
令人惊叹的!谢谢!我知道这只是一件小事。非常感谢你的帮助!