在DialogFragment YouTubePlayerFragment生命周期(YouTubeP

2019-10-18 04:55发布

我试图嵌入YouTubePlayerFragment成DialogFragment 。 我能够开始对话一次,并显示在YouTubePlayer,但第二次就老是死机(不管我做什么)。 我认为这是一个生命周期的问题,我只是不明白。 我使用AndroidAnnotations和问题是DialogFragment的视图向onCreateView方法,其通过产生AndroidAnnotations总是创建。

有谁知道如何处理DialogFragment的生命周期在这种情况下?

这是从AndroidAnnotations生成的代码:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    contentView_ = super.onCreateView(inflater, container, savedInstanceState);
    if (contentView_ == null) {
        contentView_ = inflater.inflate(layout.video_fragment, container, false);
    }
    return contentView_;
}

这是我到目前为止有:

public class VideoFragmentDialog extends DialogFragment implements YouTubePlayer.OnInitializedListener {

    private static final String DEVELOPER_KEY = "secret";
    private String videoUrl;

    @FragmentById(R.id.youTubePlayerFragment)
    YouTubePlayerFragment youTubePlayerFragment;

    @AfterViews
    void initializeYouTubePlayer() {
        youTubePlayerFragment.setRetainInstance(true);
        youTubePlayerFragment.initialize(DEVELOPER_KEY, this);
    }

    @Override
    public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer youTubePlayer, boolean wasRestored) {
        if (!wasRestored) {
            youTubePlayer.cueVideo(videoUrl);
        }
    }

    @Override
    public void onInitializationFailure(YouTubePlayer.Provider provider, YouTubeInitializationResult youTubeInitializationResult) {
        //To change body of implemented methods use File | Settings | File Templates.
    }

    public String getVideoUrl() {
        return videoUrl;
    }

    public void setVideoUrl(String videoUrl) {
        this.videoUrl = videoUrl;
    }
}

这是堆栈跟踪:

Caused by: java.lang.IllegalArgumentException: Binary XML file line #10: Duplicate id 0x7f0a0281, tag null, or parent id 0x7f0a0280 with another fragment for com.google.android.youtube.player.YouTubePlayerFragment
    at android.app.Activity.onCreateView(Activity.java:4248)
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:673)

Answer 1:

我想这是因为你使用的是片段(嵌套片段)内的片段,而无需使用getChildFragment()在这里寻找一个例子,如何做到这一点: 嵌套片段



文章来源: YouTubePlayerFragment lifecycle in a DialogFragment