title: YouTubePlayerFragment in ListView with AppCompatActivity Error
body: <p>I am developing one android application. In this application I want youtube video play with youtube player in <code>ListView</code> or <code>RecyclerView</code> with <code>AppCompatActivity</code>. I have used <code>YouTubeBaseActivity</code> it is working well with this, but I need <code>AppCompatActivity</code>. So I used <code>YouTubePlayerFragment</code>or <code>YouTubePlayerSupportFragment</code> for solving my problem.</p>
  <p>When I am using <code>YouTubePlayerFragment</code> with <code>AppCompatActivity</code> then its working well. But I want to use this in <code>ListView</code>, but I'm getting following error.</p>
  <pre><code> android.view.InflateException: Binary XML file line #8: Binary XML file line #8: Error inflating class fragment
                                                                               Caused by: android.view.InflateException: Binary XML file line #8: Error inflating class fragment
                                                                               Caused by: java.lang.IllegalArgumentException: Binary XML file line #8: Duplicate id 0x7f0b0063, tag null, or parent id 0x7f0b0062 with another fragment for com.google.android.youtube.player.YouTubePlayerFragment
                                                                                   at android.app.FragmentManagerImpl.onCreateView(FragmentManager.java:3611)
                                                                                   at android.app.FragmentController.onCreateView(FragmentController.java:98)
                                                                                   at android.app.Activity.onCreateView(Activity.java:6182)
                                                                                   at android.support.v4.app.BaseFragmentActivityApi14.onCreateView(BaseFragmentActivityApi14.java:41)
                                                                                   at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:75)
                                                                                   at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:783)
                                                                                   at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:733)
                                                                                   at android.view.LayoutInflater.rInflate(LayoutInflater.java:866)
                                                                                   at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:827)
                                                                                   at android.view.LayoutInflater.inflate(LayoutInflater.java:518)
                                                                                   at android.view.LayoutInflater.inflate(LayoutInflater.java:426)
                                                                                   at android.view.LayoutInflater.inflate(LayoutInflater.java:377)
                                                                                   at com.example.youtubecardview.frag_youtube.custom_youtube_list_adapter.getView(custom_youtube_list_adapter.java:63)
                                                                                   at android.widget.AbsListView.obtainView(AbsListView.java:2372)
                                                                                   at android.widget.ListView.measureHeightOfChildren(ListView.java:1408)
                                                                                   at android.widget.ListView.onMeasure(ListView.java:1315)
                                                                                   at android.view.View.measure(View.java:21783)
                                                                                   at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:715)
                                                                                   at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:461)
                                                                                   at android.view.View.measure(View.java:21783)
                                                                                   at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6549)
                                                                                   at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
                                                                                   at android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:139)
                                                                                   at android.view.View.measure(View.java:21783)
                                                                                   at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6549)
                                                                                   at android.support.v7.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:391)
                                                                                   at android.view.View.measure(View.java:21783)
                                                                                   at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6549)
                                                                                   at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
                                                                                   at android.view.View.measure(View.java:21783)
                                                                                   at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6549)
                                                                                   at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1514)
                                                                                   at android.widget.LinearLayout.measureVertical(LinearLayout.java:806)
                                                                                   at android.widget.LinearLayout.onMeasure(LinearLayout.java:685)
                                                                                   at android.view.View.measure(View.java:21783)
                                                                                   at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6549)
                                                                                   at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
                                                                                   at com.android.internal.policy.DecorView.onMeasure(DecorView.java:699)
                                                                                   at android.view.View.measure(View.java:21783)
                                                                                   at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2409)
                                                                                   at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1497)
                                                                                   at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1750)
                                                                                   at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1385)
                                                                                   at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6722)
                                                                                   at android.view.Choreographer$CallbackRecord.run(Choreographer.java:886)
                                                                                   at android.view.Choreographer.doCallbacks(Choreographer.java:698)
                                                                                   at android.view.Choreographer.doFrame(Choreographer.java:633)
                                                                                   at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:872)
                                                                                   at android.os.Handler.handleCallback(Handler.java:769)
                                                                                   at android.os.Handler.dispatchMessage(Handler.java:98)
                                                                               
  </code></pre>
  <p><strong>1. Main2Acitivy.java</strong></p>
  <pre><code>public class Main2Activity extends AppCompatActivity {
  
      private ListView mList;
      private Context mContext;
      private Activity mActivity;
  
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main2);
  
          mContext = this;
          mActivity = this;
  
          mList = (ListView) findViewById(R.id.list_view);
  
          List&lt;String&gt; mVideoIDList = new ArrayList&lt;&gt;();
          mVideoIDList.add(&quot;KSGYVl4ZgRs&quot;);
         // mVideoIDList.add(&quot;nCgQDjiotG0&quot;);
         // mVideoIDList.add(&quot;P3mAtvs5Elc&quot;);
  
  
          mList.setAdapter(new custom_youtube_list_adapter(mContext,mVideoIDList,mActivity));
  
  
          /**
           * @usages
           * check YouTubePlayerFragment is working with main activity or not
           *
           * @status  :  #Working
           */
  
          /*
           YouTubePlayerFragment youtubeFragment = (YouTubePlayerFragment) getFragmentManager().findFragmentById(R.id.youtubeplayerfragment);
           youtubeFragment.initialize(YOUTUBE_API_KEY,
                  new YouTubePlayer.OnInitializedListener() {
                      @Override
                      public void onInitializationSuccess(YouTubePlayer.Provider provider,
                                                          YouTubePlayer youTubePlayer, boolean b) {
                          // do any work here to cue video, play video, etc.
                          youTubePlayer.cueVideo(&quot;KSGYVl4ZgRs&quot;);
                      }
                      @Override
                      public void onInitializationFailure(YouTubePlayer.Provider provider,
                                                          YouTubeInitializationResult youTubeInitializationResult) {
  
                      }
                  });*/
  
      }
  }
  </code></pre>
  <p><strong>2. activity_main2.xml</strong></p>
  <pre><code>&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
  &lt;RelativeLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
      xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
      xmlns:tools=&quot;http://schemas.android.com/tools&quot;
      android:layout_width=&quot;match_parent&quot;
      android:layout_height=&quot;match_parent&quot;
      tools:context=&quot;com.example.youtubecardview.frag_youtube.Main2Activity&quot;&gt;
  
      &lt;ListView
          android:layout_width=&quot;wrap_content&quot;
          android:id=&quot;@+id/list_view&quot;
          android:layout_height=&quot;wrap_content&quot;&gt;
  
      &lt;/ListView&gt;
  
  &lt;/RelativeLayout&gt;
  </code></pre>
  <p><strong>3. custom_youtube_list_adapter.java</strong></p>
  <pre><code>public class custom_youtube_list_adapter extends BaseAdapter {
  
      private List&lt;String&gt; aList;
      private Context aContext;
      private Activity aActivity;
      public custom_youtube_list_adapter(Context mContext, List&lt;String&gt; mList, Activity mActivity){
  
          aList = new ArrayList&lt;&gt;();
          this.aContext = mContext;
          this.aList = mList;
          this.aActivity =mActivity;
  
      }
  
      @Override
      public int getCount() {
          return aList.size();
      }
  
      @Override
      public String getItem(int i) {
          return aList.get(i);
      }
  
      @Override
      public long getItemId(int i) {
          return i;
      }
  
      @Override
      public View getView(int position, View convertView, ViewGroup parent) {
       mHolder  holder;
  
  
          LayoutInflater inflater = (LayoutInflater) aContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  
          View v;
  
          if (convertView == null) {
              v =inflater.from(aContext).inflate(R.layout.custom_frag_youtube_list, null);
              //v = inflater.inflate(R.layout.custom_frag_youtube_list, null);
          } else {
              v = convertView;
          }
  
          YouTubePlayerFragment youtubeFragment = (YouTubePlayerFragment) aActivity.getFragmentManager().findFragmentById(R.id.youtubeplayerfragment1);
  
          youtubeFragment.initialize(YOUTUBE_API_KEY,
                  new YouTubePlayer.OnInitializedListener() {
                      @Override
                      public void onInitializationSuccess(YouTubePlayer.Provider provider,
                                                          YouTubePlayer youTubePlayer, boolean b) {
                          // do any work here to cue video, play video, etc.
                          youTubePlayer.cueVideo(aList.get(position));
                      }
                      @Override
                      public void onInitializationFailure(YouTubePlayer.Provider provider,
                                                          YouTubeInitializationResult youTubeInitializationResult) {
  
                      }
                  });
  
          return v;
      }
  
      class mHolder {
  
      }
  }
  </code></pre>
  <p><strong>4. custom_frag_youtube_list.xml</strong></p>
  <pre><code>&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
  &lt;RelativeLayout
      xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
  android:id=&quot;@+id/parent_relativeLayout&quot;
  android:layout_width=&quot;match_parent&quot;
  android:layout_height=&quot;wrap_content&quot; &gt;
  
          &lt;fragment
              android:name=&quot;com.google.android.youtube.player.YouTubePlayerFragment&quot;
              android:id=&quot;@+id/youtubeplayerfragment1&quot;
              android:layout_width=&quot;match_parent&quot;
              android:layout_height=&quot;wrap_content&quot;/&gt;
  
  &lt;/RelativeLayout&gt;
  </code></pre>
  <p>So, please suggest me where am I wrong? How to solve my problem?</p>
  <p>Thanks in advance.</p>
  <hr />
  <p><strong>UPDATE 1:</strong></p>
  <p>I am using @azizbekian solution.This is solve my problem inflate exception but I have same video with multiple layout.</p>
  <pre><code>@Override
      public View getView(final int position, View convertView, ViewGroup parent) {
  
          View view;
          YouTubePlayerFragment youtubeFragment;
          LayoutInflater inflater = (LayoutInflater) aContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  
          if (convertView == null) {
              View temp = inflater.inflate(R.layout.custom_frag_youtube_list, parent, false);
              final int id = View.generateViewId();
              temp.setId(id);
              youtubeFragment = YouTubePlayerFragment.newInstance();
              aActivity.getFragmentManager()
                      .beginTransaction()
                      .replace(id, youtubeFragment)
                      .commit();
              view = temp;
              view.setTag(youtubeFragment);
          } else {
              view = convertView;
              youtubeFragment = (YouTubePlayerFragment)view.getTag();
          }
  
          youtubeFragment.initialize(YOUTUBE_API_KEY,
                  new YouTubePlayer.OnInitializedListener() {
                      @Override
                      public void onInitializationSuccess(YouTubePlayer.Provider provider,
                                                          YouTubePlayer youTubePlayer, boolean b) {
                          // do any work here to cue video, play video, etc.
                        Log.e(&quot;Youtube Position: &quot;,&quot;&quot;+position);
                        Log.e(&quot;Youtube list value: &quot;,&quot;&quot;+aList.get(position));
                        youTubePlayer.cueVideo(aList.get(position));
                          }
                      @Override
                      public void onInitializationFailure(YouTubePlayer.Provider provider,
                                                          YouTubeInitializationResult youTubeInitializationResult) {
  
                      }
                  });
  
  
              return view;
        }
  </code></pre>
  <p><strong>Log Value:</strong></p>
  <blockquote>
  <p>Youtube Position:: 0</p>
  <p>Youtube list value:: KSGYVl4ZgRs</p>
  <p>Youtube Position:: 1</p>
  <p>Youtube list value:: nCgQDjiotG0</p>
  </blockquote>
  <hr />
  <p><strong>UPDATE 2:</strong></p>
  <p>I have resolve all problem instead of one problem. if i play video after playing one video that time adapter not refresh. so that i am unable to handle video playing at one time.</p>
  <p><strong>UPDATE 3:</strong></p>
  <p>After lot of searching and @azizbekian soluion,  I have resolved my problem.  by using following solution.</p>
  <pre><code> @Override
      public View getView(final int position, View convertView, final ViewGroup parent) {
  
          final mHolder holder;
          final View fview;
          final LayoutInflater inflater = (LayoutInflater) aContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  
          if (convertView == null) {
  
              holder = new mHolder();
              fview = inflater.inflate(R.layout.custom_youtube_frag_listview_list, parent, false);
              holder.mLoaderFrame = (ImageView) fview.findViewById(R.id.imgLayer1);
              fview.setTag(holder);
  
          } else {
              fview = convertView;
              holder = (mHolder) fview.getTag();
          }
  
          String VideoID = aList.get(position);
  
          /**
          * @usages
          * https://img.youtube.com/vi/VideoID/0.jpg
          * 
          * where 
            VideoID is ur youtube video id
            YOUTUBE_THUMBNAIL_URL_VIDEO1  = https://img.youtube.com/vi/
            YOUTUBE_THUMBNAIL_URL_VIDEO2  = /0.jpg
          */
  
          String mURL = YOUTUBE_THUMBNAIL_URL_VIDEO1 + VideoID + YOUTUBE_THUMBNAIL_URL_VIDEO2;
  
          Glide.with(aContext).load(mURL).into(holder.mLoaderFrame);
          holder.mLoaderFrame.setVisibility(View.VISIBLE);
  
          if(mCounterLeaveremove != 0){
              int mId = position+1;
  
              if(mCounterLeaveremove != mId){
                  FragmentManager fragmentManager = aActivity.getFragmentManager();
                  Fragment oldFragment = null;
                  try {
                      oldFragment = fragmentManager.findFragmentById(mId);
                  } catch (Exception e) {
                      e.printStackTrace();
                  }
                  if(oldFragment != null) {
                      // Delete fragmet from ui, do not forget commit() otherwise no action
                      // is going to be observed
                      fragmentManager.beginTransaction().remove(oldFragment).commit();
                  }
              }
  
          }
  
          holder.mLoaderFrame.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View view) {
  
                  mCounterLeaveremove = position+1;
  
                  notifyDataSetChanged();
  
                  YouTubePlayerFragment youtubeFragment;
  
                  final int id = position+1;
                  fview.setId(id);
                  youtubeFragment = YouTubePlayerFragment.newInstance();
  
                  aActivity.getFragmentManager()
                          .beginTransaction()
                          .replace(id, youtubeFragment)
                          .commit();
  
                  youtubeFragment.initialize(YOUTUBE_API_KEY,
                          new YouTubePlayer.OnInitializedListener() {
                              @Override
                              public void onInitializationSuccess(YouTubePlayer.Provider provider,
                                                                  YouTubePlayer youTubePlayer, boolean b) {
  
                                  Log.e(&quot;Youtube Position: &quot;, &quot;&quot; + position);
                                  Log.e(&quot;Youtube list value: &quot;, &quot;&quot; + aList.get(position));
  
                                  holder.mLoaderFrame.setVisibility(View.GONE);
  
                                  String VideoID = aList.get(position);
  
                                  youTubePlayer.loadVideo(VideoID);
                                  youTubePlayer.setOnFullscreenListener(new YouTubePlayer.OnFullscreenListener() {
                                      @Override
                                      public void onFullscreen(boolean b) {
  
                                      }
                                  });
  
                                  youTubePlayer.addFullscreenControlFlag(0);
  
                                  /**
                                   * @usages
                                   * 1. Listener use for manage video thumbnail and youtube player
                                   */
                                  new custom_youtube_list_adapter.MyPlayerStateChangeListener(holder.mLoaderFrame);
                                  new custom_youtube_list_adapter.MyPlaybackEventListener(holder.mLoaderFrame);
                              }
  
                              @Override
                              public void onInitializationFailure(YouTubePlayer.Provider provider,
                                                                  YouTubeInitializationResult youTubeInitializationResult) {
  
                              }
                          });
  
              }
          });
  
          return fview;
      }
  </code></pre>
  <p>For getting youtube video thumnail, you can go throw.</p>
  <p><a href="https://stackoverflow.com/questions/2068344/how-do-i-get-a-youtube-video-thumbnail-from-the-youtube-api">How do I get a YouTube video thumbnail from the YouTube API?</a></p>