2012-12-07 2 views
2

나는 Listfragment 일하고 작동하지 않습니다. 활동에 중첩 클래스가 있습니다. 유형 불일치 : 내가 직접 DummySectionFragment을 할 때 MainActivity.DummySectionFragment에서 변환 할 수 없습니다하는안드로이드 단편 프레임 워크 : 최대 캐스팅이 활동 안에

을 단편

public static class DummySectionFragment extends ListFragment { 
    /** 
    * The fragment argument representing the section number for this 
    * fragment. 
    */ 
    public static final String ARG_SECTION_NUMBER = "section_number"; 

    public DummySectionFragment() { 
    } 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
      Bundle savedInstanceState) { 

     View contentView=inflater.inflate(R.layout.activity_main_fragment_browse, container); 
     return contentView; 
    } 

하지만 컴파일러는 컴파일러에서 불평 DummySectionFragemt이 조각

@Override 
public void onTabSelected(ActionBar.Tab tab, 
     FragmentTransaction fragmentTransaction) { 
    // When the given tab is selected, show the tab contents in the 
    // container view. 
    Fragment fragment = new DummySectionFragment(); // <-- complain not fragment 
    // codes omitted ....... 
} 

없는 불평 Fragment를 확장하면 작동하지만 이전에 작동하지 않는 이유를 이해하지 못합니다. 그것이 작동하지 않는 이유를 분명히, DummySectionFragment이 Fragment.It 여기 암시 업 캐스팅을해야 확장 ListFragment가 확장, 이해가 안 :(

//make it directly extends Fragment --> 
public static class DummySectionFragment extends Fragment { 
    /** 
    * The fragment argument representing the section number for this 
    * fragment. 
    */ 
    public static final String ARG_SECTION_NUMBER = "section_number"; 

    public DummySectionFragment() { 
    } 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
      Bundle savedInstanceState) { 

     View contentView=inflater.inflate(R.layout.activity_main_fragment_browse, container); 
     return contentView; 
    } 
+2

당신이에서 조각 클래스를 사용하고 있는지, 당신이 경우는 지원 라이브러리의 ListFragment ('android.support.v4.app.Fragment') 사용 여부를하고 있는지 확인 지원 라이브러리뿐만 아니라. – dmon

+0

내가 두 패키지는 같은 클래스와있다 몰랐어요. 대단히 감사합니다. – user1886616

+0

을 user1886616 그래 @,이 모두를 얻을 생각 . Google은 ADT 17 (?)으로 만든 모든 새 앱이 자동으로 지원 라이브러리를 사용한다는 것을 기본 설정하는 것이 좋습니다. – AedonEtLIRA

답변

0
public static class DummySectionFragment extends ListFragment { 
    /** 
    * The fragment argument representing the section number for this 
    * fragment. 
    */ 
    public static final String ARG_SECTION_NUMBER = "section_number"; 

    public DummySectionFragment() { 
    } 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
      Bundle savedInstanceState) { 
     View rootView; 
     if(Integer.toString(getArguments().getInt(ARG_SECTION_NUMBER)).equals("1")){ 
      rootView = inflater.inflate(R.layout.fragment_tab1,container, false);    
     } 
     else if(Integer.toString(getArguments().getInt(ARG_SECTION_NUMBER)).equals("2")){ 
      rootView = inflater.inflate(R.layout.fragment_tab2,container, false);    
     } 
     else if(Integer.toString(getArguments().getInt(ARG_SECTION_NUMBER)).equals("3")){ 
      rootView = inflater.inflate(R.layout.fragment_tab3,container, false);    
     } 
     else{ 
      rootView = inflater.inflate(R.layout.fragment_tab,container, false); 
     } 
     return rootView; 
    } 
} 

// 당신이 수입 android.support를 사용해야합니다. v4.app.ListFragment ->