2014-02-16 98 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