2013-02-21 45 views
-1

嘿!SherlockListFragment中的ExpandableListView使用擴展BaseExpandableListAdapter的自定義適配器

我有這個設置。

MainActivity - 延伸SherlockFragmentActivity - 顯示4個片段使用TabsAdapter

我有那些我需要這個選項卡之一的問題:

FragmentSkills - 延伸SherlockListFragment - 顯示的一些的ExpandableListView現在硬編碼測試字符串

========================================= ========================================= 問題: 我正在此時一個NullPointerException,我似乎無法旋轉:

//我onActivityCreated()在FragmentSkills.java 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; 
    } 
} 

回答

0

首先用FragmentSkills extends SherlockFragment代替FragmentSkills extends SherlockListFragment,這裏你不需要SherlockListFragment。在public void onActivityCreated(Bundle savedInstanceState)

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
     Bundle savedInstanceState) { 
    return inflater.inflate(R.layout.fragment_skills, container, false); 
} 

也代替

expandList = (ExpandableListView) getActivity().findViewById(R.id.ExpList); 

做到這一點

expandList = (ExpandableListView) getView().findViewById(R.id.ExpList); 
+0

真棒:現在您的FragmentSkills添加此方法!非常感謝!我知道這只是一些小事。非常感謝您的幫助! – noneofthem 2013-02-21 20:13:17

相關問題