0

xml 레이아웃에서 ListFragment를 찾으려고하면 항상 null이 반환됩니다. SherlockFragmentActivity를 SherlockListFragment와 함께 사용하고 있습니다.findFragmentById는 항상 null입니다.

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.my_custom_layout); 

     adapter = new My_Custom_Adapter(this, My_Object, My_Object_2); 

    } 

// Create the list fragment and add it as the list content. 
    if (getSupportFragmentManager().findFragmentById(android.R.id.content) == null) { 
     mListFragment list = new mListFragment(); 
     getSupportFragmentManager().beginTransaction().add(android.R.id.content, list).commit(); 

    } 
} 

public static class mListFragment extends SherlockListFragment { 

    @Override 
    public void onActivityCreated(Bundle savedInstanceState) { 
     super.onActivityCreated(savedInstanceState); 
     setListAdapter(adapter); 
     setListShown(true); 
    } 

} 

이것은 XML 레이아웃의 단축 버전입니다 :

<RelativeLayout 
    android:id="@+id/top_control_bar"> 
    <!-- Text Views --> 
</RelativeLayout> 
<RelativeLayout > 
    <!-- Button --> 
</RelativeLayout> 
<LinearLayout 
    android:id="@+id/start_data" 
    android:layout_width="match_parent" 
    android:layout_height="fill_parent" 
    android:layout_below="@id/top_control_bar" 
    android:orientation="vertical" > 

    <fragment 
    android:name="android.support.v4.app.ListFragment" 
    android:id="@android:id/content" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    /> 
</LinearLayout> 

findFragmentById(android.R.id.content) 

는 항상 null을 반환 여기에 코드입니다. 내 레이아웃 내에서 조각을 찾을 수없는 이유는 무엇입니까?

답변

0

android:id="@android:id/content" 

android:id="@+id/content" 

있어야하지 않나요?

documentation에서 잘못된 구문을 사용하고있는 것처럼 보입니다.

필자의 연결 방법이 올바르게 작성되었는지 확신 할 수 없습니다. 그렇다하더라도

getSupportFragmentManager().beginTransaction().add(android.R.id.content, list).commit(); 

FragmentTransaction transaction = getFragmentManager().beginTransaction(); 
transaction.add(android.R.id.content, list); 
transaction.commit(); 

에 변경, 솔직히 당신이 뭘하려고 할 것입니다 모르겠어요. FragmentTransaction's add method as you're using it이 if에 이미 null 값을 갖는 것으로 설정된 컨테이너에 조각을 추가하려고한다는 것을 알고 있습니까?

+0

:. 당신이 시도해보십시오 SupportMapFragment

가 필요합니다, 그래서

에서 getSupportFragmentManager() findFragmentById (android.R.id.content)는 지원 조각 관리자를 사용합니다. – DaveSav

+0

무슨 뜻인가요? [문서] [1]에서 잘못된 구문을 사용하고있는 것처럼 보입니다. [1] : http://developer.android.com/guide/components/fragments.html#Adding – DigCamara

+0

xml을 제안 사항으로 변경했지만 불행하게도 아무런 변화가 없었습니다. – DaveSav

1

XML에서 ListFragment로 Fragment를 선언했기 때문에 문제가 발생했습니다. 그것은 아니, 보일 것

<fragment 
    android:name="com.google.android.gms.maps.SupportMapFragment" 
    android:id="@android:id/content" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
/>