如何解决未附加到活动的片段呢?

时间:2017-12-10 19:25:23

标签: android android-fragments youtube

我正在使用扩展AppCompatDialogFragment的类,类代码:

public class YoutubeDialogFragment extends AppCompatDialogFragment {

@SuppressLint("StaticFieldLeak")
private static View rootView;

@SuppressLint("ValidFragment")
YoutubeDialogFragment() {

}

public static YoutubeDialogFragment newInstance(String title) {
    YoutubeDialogFragment youtubeDialogFragment = new YoutubeDialogFragment();
    Bundle bundle = new Bundle();
    bundle.putString("title", title);
    youtubeDialogFragment.setArguments(bundle);
    return youtubeDialogFragment;
}

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    if (rootView == null) {
        rootView = inflater.inflate(R.layout.general_pop_up_youtube, container, false);
    }
    return rootView;
}

布局general_pop_up_youtube包含一个片段,我试图使用getChildFragmentManager()获取该片段,但它总是抛出以下异常:

RuntimeException: Fragment has not been attached yet.

我使用此代码启动片段:

YoutubeDialogFragment popupYouTubePlayerFragment = YoutubeDialogFragment.newInstance("Title");

                FragmentManager fragmentManager = activity.getSupportFragmentManager();

                popupYouTubePlayerFragment.show(fragmentManager.beginTransaction(), "youtube");

                popupYouTubePlayerFragment.getChildFragmentManager()
                        .beginTransaction()
                        .add(YouTubePlayerSupportFragment.newInstance(), "youtube_frag")
                        .commit();

有什么想法吗?

0 个答案:

没有答案