findFragmentById는 항상 null을 반환합니다.
xml 레이아웃에서 내 조각의 ID를 정의하고 있습니다.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/test_fragment"
...
그런 다음 활동의 onCreate 메서드에이 조각을 추가합니다.
MyFragment myFragment = new MyFragment();
fragmentTransaction.add(R.id.fragment_container, myFragment);
fragmentTransaction.commit();
이것은 모두 잘 작동합니다. 조각을 교체하고 작동합니다.
나중에 활동의 메서드 중 하나에서 ID로이 조각을 검색하려고합니다.
MyFragment myFragment = (MyFragment) getFragmentManager().findFragmentById(R.id.test_fragment);
그렇게하면 myFragment가 null됩니다. 항상.
ID 대신 태그를 사용하여 동일한 작업을 수행하려고하면 문제없이 태그로 조각을 검색 할 수 있습니다.
MyFragment myFragment = new MyFragment();
fragmentTransaction.add(R.id.fragment_container, myFragment, "testfragment");
fragmentTransaction.commit();
...
MyFragment myFragment = (MyFragment) getFragmentManager().findFragmentByTag("testfragment");
findFragmentById가 조각을 찾을 수 없지만 findFragmentByTag가 찾는 이유는 무엇입니까? 내가 뭔가를 놓치고 있습니까?
R.id.test_fragment 조각의 ID가 아니라 LinearLayout의 ID입니다.
호출 add(int containerViewId, Fragment fragment)하면 태그없이 조각이 추가됩니다. 그래서 또는 사용 add(int containerViewId, Fragment fragment, String tag)하고 태그를 사용하여 조각을 다시 가져옵니다 (ID로)
<FrameLayout>태그를 레이아웃 파일의 컨테이너로 사용하십시오 . 나중에 활동의 조각을 동적으로 <FrameLayout>바꾸기 위해 컨테이너 의 ID를 사용 하여 활동의 조각을 바꿀 수 있습니다.
<FrameLayout
android:id="@+id/test_fragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/test_fragment"
그것은 fragment아니야LinearLayout
<fragment android:name="com.example.yourfragment"
android:id="@+id/test_fragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
실제로 하위 클래스를 검색 android.support.v4.app.Fragment하는 호출 getFragmentManager()하는 동안 내 레이아웃에서 사용 하고 있었고 . 그래서 수정은 대신 전화하는 것이 었습니다 .android.app.FragmentnullgetSupportFragmentManager()
일반적으로 레이아웃에서 서브 클래 싱하고 사용하는 조각의 패키지가 FragmentManager검색을 수행 하는 해당 패키지가 반환 한 것과 동일한 지 확인 합니다.
R.id.test_fragmentLinearLayout당신의 조각이 아니라 당신 의 ID입니다.
이 샘플 http://developer.android.com/guide/topics/fundamentals/fragments.html#Adding 과 같이 XML에서 부 풀릴 때 조각에 및 ID를 정의 할 수 있습니다.
또는 대신 다음을 사용해야합니다.
(MyFragment) getFragmentManager().findFragmentById(R.id.fragment_container);
FragmentB fragmentB =
(FragmentB) getSupportFragmentManager().findFragmentById(R.id.containerb);
if(fragmentB != null)
{
fragmentB.showuserResponse(msg);
}
컨테이너 ID를 조각 ID로 사용합니다. 그런 다음 null 참조를 확인하십시오.
fragmentTransaction.add (R.id.fragment_container, myFragment);
MyFragment myFragment = (MyFragment) getFragmentManager (). findFragmentById (R.id.test_fragment);
차이점을 확인하십시오.
R.id.fragment_container를 인수로 findFragmentById전달해야합니다. 대신 add 함수에 전달해야합니다.R.id.test_fragment
그건 그렇고, 두 함수의 내부 구현에 따르면 id가 컨테이너 뷰의 ID가 될 수 있다는 것이 옳을 것입니다.
참조 URL : https://stackoverflow.com/questions/10833666/findfragmentbyid-always-returns-null
'IT Share you' 카테고리의 다른 글
| 외래 키 속성의 기본값 설정 (0) | 2021.01.08 |
|---|---|
| $의 차이? (0) | 2021.01.08 |
| 라우팅 앱 커버리지 파일 (0) | 2021.01.08 |
| 측정 프로토콜을 통해 Google Analytics에 추적 데이터를 보낼 때 클라이언트 ID는 무엇입니까? (0) | 2021.01.08 |
| Xcode 7.3 : 취소 선으로 표시된 모듈 가져 오기 (0) | 2021.01.08 |