1
我試圖將YouTubePlayerFragment嵌入到DialogFragment中。我能夠一次啓動對話框並顯示YouTubePlayer,但第二次它總是崩潰(不管我做什麼)。我認爲這是一個生命週期問題,我根本不明白。 我正在使用AndroidAnnotations,問題是DialogFragment的視圖始終在由AndroidAnnotations生成的onCreateView方法中創建。YouTubePlayerFragment生命週期中的對話框片段
有沒有人知道在這種情況下如何處理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)
我不認爲這是問題,因爲我已經在另一個片段內使用了ViewPagerFragment的嵌套片段。 –