为什么getSupportFragmentManager()。findFragmentById返回null

时间:2017-10-11 20:39:58

标签: android android-fragments

来自OverlayActivity

@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();
}

最重要的是:

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>

3 个答案:

答案 0 :(得分:0)

fragment_full_screen_video_player_underlay_id

是您RelativeLayout的ID。 来自documentation

  

findFragmentById

     

在从XML中膨胀时查找由给定ID标识的片段,或者在事务中添加时查找容器ID。这首先搜索当前添加到经理活动中的片段;如果没有找到这样的片段,则搜索当前在与该ID相关联的后栈上的所有片段。

这意味着您的fragment要么从XML中膨胀 - 在这种情况下,您必须使用<Fragment>元素的ID(如果有的话),或者使用具有托管给定ID的容器片段。

如果您使用交易添加了片段,并且需要稍后检索它,请使用findFragmentByTag (String tag)并使用add添加片段

 add (int containerViewId, Fragment fragment, String tag)

答案 1 :(得分: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

答案 2 :(得分: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(); } 中添加一个框架布局,我在这个框架布局中加载了一个片段,这是一个函数。

{{1}}

这对我很有用,我希望对你有同感...... 快乐的编码。 :)