2017-02-13 25 views
1

对不起,我的英语。我需要动态地创建YouTubePlayer,我不能扩展YoutubeBaseActivity。所以我只需要创建YouTubePlayerSupportFragment。我尝试做这样的创建YouTubePlayerSupportFragment programaticaly(android)

container_more_info_item - 它在我的LinearLayout

//create video 
     for(int j = 0; j < 5; j++) { 
      final YouTubePlayerSupportFragment ysF = new YouTubePlayerSupportFragment(); 
      ysF.initialize("AIzbSyAT5C-xs5YG1HzG69Fn__PHq9DHXBuKpws", new YouTubePlayer.OnInitializedListener() { 
       @Override 
       public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer youTubePlayer, boolean b) { 
        youTubePlayer.cueVideo("IjUlQU6yifk"); 
       } 

       @Override 
       public void onInitializationFailure(YouTubePlayer.Provider provider, YouTubeInitializationResult youTubeInitializationResult) { 

       } 
      }); 

//this line error, because ViewGroup can not be applied com.google.android.youtube.player.YouTubePlayerSupportFragment 
      container_more_info_item.addView(ysF); 
     } 

,现在我卡住了。不能拿出一些

UPD:

我使用FragmentManager,但我有这个消息Cannot resolve method 'add(int, com.google.android.youtube.player.YouTubePlayerSupportFragment)

//create video 
     for(int j = 0; j < 5; j++) { 
      final YouTubePlayerSupportFragment ysF = new YouTubePlayerSupportFragment(); 
      ysF.initialize("AIzaSyAT5C-xs5YG1HzG69Fn__PHq9DHXBuKpws", new YouTubePlayer.OnInitializedListener() { 
       @Override 
       public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer youTubePlayer, boolean b) { 
        youTubePlayer.cueVideo("IjUlQU6yifk"); 
       } 

       @Override 
       public void onInitializationFailure(YouTubePlayer.Provider provider, YouTubeInitializationResult youTubeInitializationResult) { 

       } 
      }); 

      LinearLayout ll = new LinearLayout(this); 
      ll.setOrientation(LinearLayout.HORIZONTAL); 

      ll.setId(j); 

      FragmentManager fragmentManager = getFragmentManager(); 
      FragmentTransaction fragTransaction = fragmentManager.beginTransaction(); 
//Cannot resolve method 'add(int, com.google.android.youtube.player.YouTubePlayerSupportFragment) 
      fragTransaction.add(ll.getId(), ysF); 
      fragTransaction.commit(); 

      container_more_info_item.addView(ll); 
     } 
+1

要添加一个片段,你应该使用'FragmentManager':https://developer.android.com/guide/components/fragments.html#Transactions –

+0

@KenWolf感谢您的评论,我更新了我的问题。请帮我做正确的事情? – g71132

+1

使用'getSupportFragmentManager()'而不是'getFragmentManager()' –

回答