從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>
你可以發佈活動的XML嗎? –