【问题标题】:ExpandableListView in SherlockListFragment with custom Adapter that extends BaseExpandableListAdapterSherlockListFragment 中的 ExpandableListView 与扩展 BaseExpandableListAdapter 的自定义适配器
【发布时间】:2013-02-21 19:14:04
【问题描述】:

你好!

我有这个设置。

主活动 - 扩展 SherlockFragmentActivity - 使用 TabsAdapter 显示 4 个片段

我在需要此选项卡的某个选项卡上遇到问题:

片段技能 - 扩展 SherlockListFragment - 现在显示一些硬编码测试字符串的 ExpandableListView

================================================ ==================================== 问题: 此时我得到一个 NullPointerException,我似乎无法旋转:

// 在 FragmentSkills.java 的 onActivityCreated() 中 expandList = (ExpandableListView) getActivity().findViewById(R.id.ExpList);

================================================ ====================================

我有点卡在这里,我不知道还能尝试什么。我发现了很多类似的例子,但它们都只展示了如何在正常的活动中做到这一点。由于我缺乏经验,这可能是我看不到的小东西,所以请看一下并尝试帮助我解决这个问题。

我是 Android 新手,所以请耐心等待。任何帮助表示赞赏。提前谢谢了。这是相关的代码。如果您需要查看更多信息,请告诉我。 非常感谢!

.

fragment_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>

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: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;
    }
}

【问题讨论】:

    标签: android listview actionbarsherlock expandablelistview


    【解决方案1】:

    首先将FragmentSkills extends SherlockListFragment替换为FragmentSkills extends SherlockFragment,这里不需要SherlockListFragment。现在将此方法添加到您的FragmentSkills

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

    【讨论】:

    • 太棒了!非常感谢!我知道这只是一件小事。非常感谢您的帮助!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多