2014-02-16 33 views
1

我試圖在fragment中加載到viewpager,但我無法加載2個或更多YouTubePlayerSupportFragmentYoutubeplayer在一個視圖片段中的片段

如果我加載2個或更多,我得到了一個錯誤:

java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first. 

但如果負載1它正常工作。

我該如何加載?

感謝所有人,對不起我的英語不好。

的XML:

<?xml version="1.0" encoding="utf-8"?> 
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" > 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:background="#FFF" 
     android:orientation="vertical" 
     android:padding="20dp" > 

     <TextView 
      android:id="@+id/tvTituloTecnica" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:textAppearance="?android:attr/textAppearanceLarge" 
      android:textColor="#045FB4" /> 

     <View 
      android:layout_width="match_parent" 
      android:layout_height="2dp" 
      android:background="#08088A" /> 

     <ImageView 
      android:id="@+id/ivTecnica" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:contentDescription="@string/app_name" /> 

     <TextView 
      android:id="@+id/tvDescripcion" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_marginTop="15dp" /> 

     <fragment 
      android:id="@+id/youtube_fragment" 
      android:name="com.google.android.youtube.player.YouTubePlayerSupportFragment" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" /> 

     <!-- <com.google.android.youtube.player.YouTubeThumbnailView --> 
     <!-- android:id="@+id/youtube_view" --> 
     <!-- android:layout_width="match_parent" --> 
     <!-- android:layout_height="match_parent" --> 
     <!-- android:layout_marginTop="15dp" /> --> 

    </LinearLayout> 

</ScrollView> 

而且代碼加載視圖:

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
     Bundle savedInstanceState) { 
// if (view == null) { 
view = inflater.inflate(R.layout.tecnica_list_item, container, false); 
// } 

Bundle b = this.getArguments(); 
tecnica = ViewPagerAdapter.tecnicas.get(b.getInt("tecnica")); 

TextView titulo = (TextView) view.findViewById(R.id.tvTituloTecnica); 
titulo.setText(tecnica.getNombre()); 

TextView descripcion = (TextView) view.findViewById(R.id.tvDescripcion); 
descripcion.setText(tecnica.getDescripcion()); 

ImageView imagenTecnica = (ImageView) view.findViewById(R.id.ivTecnica); 
imagenTecnica.setImageResource(this 
     .getActivity() 
     .getResources() 
     .getIdentifier("drawable/" + tecnica.getRutaImagen(), null, 
       this.getActivity().getPackageName())); 



this.videoId = tecnica.getUrlYoutube(); 

PlayerYouTubeFrag myFragment = PlayerYouTubeFrag.newInstance(videoId); 

this.getFragmentManager().beginTransaction() 
     .replace(R.id.youtube_fragment, myFragment).commit(); 
myFragment.init(); 

return view; 
} 

回答

3

您正在嘗試新帶防滑釘的片段連接到一個片段,而不是一個容器。 我想你正嘗試在佈局中使用當前實例化的片段,並且此佈局是ViewPager詳細頁面片段,對嗎?

您不能使用在佈局xml中聲明它們的嵌套片段。您必須在第一個片段xml上放置一個佔位符佈局,並將其注入代碼中。

一些事情,如:

... 

     <TextView 
      android:id="@+id/tvDescripcion" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_marginTop="15dp" /> 

     <FrameLayout 
      android:id="@+id/youtube_fragment" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" /> 


    </LinearLayout> 
... 

,並在ViewPager細節片段:

YouTubePlayerSupportFragment youTubePlayerFragment = YouTubePlayerSupportFragment.newInstance(); 
getChildFragmentManager().beginTransaction().replace(R.id.youtube_fragment, youTubePlayerFragment).commit(); 

... 

不要忘了與

youTubePlayerFragment.initialize(apiKey, new OnInitializedListener() { 

    @Override 
    public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer player, boolean wasRestored) { 
      // Play, cue or whatever you want to do with the player 
    } 


    ... // Implement other inteface methods here 

    }); 
+0

嗨正確initializar玩家非常感謝您!爲你的答案,併爲延誤抱歉。 它工作得很好,但是當我快速更改視圖時,youtube會中斷。 我該如何解決它? – Dracknes