2013-05-16 23 views
0

當創建與全息碎片和標籤和sectionsPagerAdapter模板項目,你得到這樣的事情使用SectionsPagerAdapter定製片段

public class SectionsPagerAdapter extends FragmentPagerAdapter { 

    public SectionsPagerAdapter(FragmentManager fm) { 
     super(fm); 
    } 

    @Override 
    public Fragment getItem(int i) { 
     Fragment fragment = new DummySectionFragment(); 
     Bundle args = new Bundle(); 
     args.putInt(DummySectionFragment.ARG_SECTION_NUMBER, i + 1); 
     fragment.setArguments(args); 
     return fragment; 
    } 

DummySectionFragment的定義:

/** 
* A dummy fragment representing a section of the app, but that simply displays dummy text. 
*/ 
public static class DummySectionFragment extends Fragment { 
    public DummySectionFragment() { 
    } 

    public static final String ARG_SECTION_NUMBER = "section_number"; 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
      Bundle savedInstanceState) { 
     TextView textView = new TextView(getActivity()); 
     textView.setGravity(Gravity.CENTER); 
     Bundle args = getArguments(); 
     textView.setText(Integer.toString(args.getInt(ARG_SECTION_NUMBER))); 
     return textView; 
    } 
} 

現在,我將這段代碼我自己的片段擴展:

@Override 
    public Fragment getItem(int i) { 
     Fragment fragment = new TargetsFragment(); 
     Bundle args = new Bundle(); 

我在自己的定義文件:

public class TargetsFragment extends Fragment { 
boolean mDualPane; 
int mCurCheckPosition = 0; 


private CheckedExpandableListAdapter expandableListAdapter; 
private ExpandableListView expandableListView; 

@Override 
public void onActivityCreated(Bundle savedInstanceState) { 
    super.onActivityCreated(savedInstanceState); 

    //TODO: serve with correct data instead of this! 
     final ArrayList<ListNode> dummyList = buildDummyData(); 
     loadHosts(dummyList); 
    } 
    [...] 

我得到的錯誤是不能將TargetsFragment轉換爲getItem中的片段。我覺得我在這裏錯過了一些簡單的東西。但是什麼?如果您需要更多代碼,請詢問。

回答

3

解決方案:仔細查看進口情況。確保你在兩個類中導入和擴展相同的片段。例如android.app.fragment或導入android.support.v4.app.Fragment;