2017-10-11 90 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(); 
} 

这对我很好,我希望你也一样... 快乐编码。 :)

相关问题