2017-10-11 91 views
0

OverlayActivity爲什麼getSupportFragmentManager()findFragmentById返回null

@Override 
public void onCreate(Bundle savedInstanceState) { 
    Log.d(TAG,"onCreate"); 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_overlay); 

    // Check whether the activity is using the layout version with 
    // the fragment_container FrameLayout. If so, we must add the first chatRootFragment 
    if (findViewById(R.id.video_player_fragment_container) != null) { 
     if (savedInstanceState != null) { 
      return; 
     } 
     FullScreenVideoPlayerUnderlayFragment mMessageListFragment = new FullScreenVideoPlayerUnderlayFragment(); 

     if (mMessageListFragment != null) { 
      FragmentTransaction transaction = getSupportFragmentManager().beginTransaction(); 
      transaction.add(R.id.video_player_fragment_container, mMessageListFragment); 
      transaction.commit(); 
     } else { 
      Log.e("OverlayActivity", "Error in creating chatRootFragment"); 
     } 
    } 

    // Instantiate a ViewPager and a PagerAdapter. 
    mPager = findViewById(R.id.pager); 
    mPagerAdapter = new ScreenSlidePagerAdapter(getSupportFragmentManager()); 
    mPager.setAdapter(mPagerAdapter); 
    mPager.setCurrentItem(1); 
} 

@Override 
public void onBackPressed() { 
    FullScreenVideoPlayerUnderlayFragment fragment = (FullScreenVideoPlayerUnderlayFragment) getSupportFragmentManager().findFragmentById(R.id.fragment_full_screen_video_player_underlay_id);//<= NULL 
    OverlayActivity.super.onBackPressed(); 
} 

的frgment:

public class FullScreenVideoPlayerUnderlayFragment extends Fragment { 
@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
    return inflater.inflate(R.layout.fragment_full_screen_video_player_underlay, container, false); 
} 

@Override 
public void onDestroy() { 
    getActivity().setResult(getActivity().RESULT_OK, getActivity().getIntent().putExtra("USER_DATA_EXTRA", "Yo User")); 
    super.onDestroy(); 
    playlistManager.invokeStop(); 
    exitFullscreen(); 
} 
} 

fragment_full_screen_video_player_underlay.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:id="@+id/fragment_full_screen_video_player_underlay_id" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="@android:color/black"> 

    <com.devbrackets.android.exomedia.ui.widget.VideoView 
     android:id="@+id/video_play_activity_video_view" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     app:useDefaultControls="true"/> 
</RelativeLayout> 

activity_overlay.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" > 

    <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     android:id="@+id/video_player_fragment_container" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" /> 

    <android.support.v4.view.ViewPager 
     xmlns:android="http://schemas.android.com/apk/res/android" 
     android:id="@+id/pager" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"/> 

</RelativeLayout> 

+0

你可以發佈活動的XML嗎? –

回答

0
fragment_full_screen_video_player_underlay_id 

是你RelativeLayout的ID。 從documentation

findFragmentById

查找所識別由給定id的片段無論是從XML或作爲交易添加時容器ID膨脹時。這首先搜索當前添加到經理活動的片段;如果沒有找到這樣的片段,則搜索當前在與該ID相關聯的背堆棧中的所有片段。

這意味着要麼你fragment從XML膨脹 - 這種情況下,你將有如有使用您<Fragment>元素的id - 或與承載片段給定ID的容器。

如果您使用事務添加您的片段,你需要再取回,然後使用findFragmentByTag (String tag),並添加使用add

add (int containerViewId, Fragment fragment, String tag) 
+0

我嘗試過使用fragment_full_screen_video_player_underlay以及fragment_full_screen_video_player_underlay_id – ishandutta2007

+0

如果您嘗試使用R.id.video_player_fragment_container添加片段,您會發現它。 該id或者是指標記或片段添加到的容器。 – Luigitni

+0

是的,它的工作原理。我很愚蠢。 – ishandutta2007

0

我應該使用

FullScreenVideoPlayerUnderlayFragment fragment = (FullScreenVideoPlayerUnderlayFragment) getSupportFragmentManager().findFragmentById(R.id. video_player_fragment_container); 

,而不是

片段
FullScreenVideoPlayerUnderlayFragment fragment = (FullScreenVideoPlayerUnderlayFragment) getSupportFragmentManager().findFragmentById(R.id.fragment_full_screen_video_player_underlay_id);//<= NULL 
0

而不是找到片段我應該做的是我會在activity_main.xml中添加一個框架佈局,並且我在該框架佈局中加載了一個片段,這是一個函數。

public void loadFragment(Fragment fragment, String title) { 

    FragmentManager fragmentManager = getSupportFragmentManager(); 
    FragmentTransaction transaction = fragmentManager.beginTransaction(); 
    transaction.replace(R.id.frameLayoutId, fragment, title); 
    transaction.commit(); 
} 

這對我很好,我希望你也一樣... 快樂編碼。 :)

相關問題