Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

YouTubePlayerFragment in ListView with AppCompatActivity Error

I am developing one android application. In this application I want youtube video play with youtube player in ListView or RecyclerView with AppCompatActivity. I have used YouTubeBaseActivity it is working well with this, but I need AppCompatActivity. So I used YouTubePlayerFragmentor YouTubePlayerSupportFragment for solving my problem.

When I am using YouTubePlayerFragment with AppCompatActivity then its working well. But I want to use this in ListView, but I'm getting following error.

 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)
                                                                             

1. Main2Acitivy.java

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<String> mVideoIDList = new ArrayList<>();
        mVideoIDList.add("KSGYVl4ZgRs");
       // mVideoIDList.add("nCgQDjiotG0");
       // mVideoIDList.add("P3mAtvs5Elc");


        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("KSGYVl4ZgRs");
                    }
                    @Override
                    public void onInitializationFailure(YouTubePlayer.Provider provider,
                                                        YouTubeInitializationResult youTubeInitializationResult) {

                    }
                });*/

    }
}

2. activity_main2.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.youtubecardview.frag_youtube.Main2Activity">

    <ListView
        android:layout_width="wrap_content"
        android:id="@+id/list_view"
        android:layout_height="wrap_content">

    </ListView>

</RelativeLayout>

3. custom_youtube_list_adapter.java

public class custom_youtube_list_adapter extends BaseAdapter {

    private List<String> aList;
    private Context aContext;
    private Activity aActivity;
    public custom_youtube_list_adapter(Context mContext, List<String> mList, Activity mActivity){

        aList = new ArrayList<>();
        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 {

    }
}

4. custom_frag_youtube_list.xml

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

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

</RelativeLayout>

So, please suggest me where am I wrong? How to solve my problem?

Thanks in advance.


UPDATE 1:

I am using @azizbekian solution.This is solve my problem inflate exception but I have same video with multiple layout.

@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("Youtube Position: ",""+position);
                      Log.e("Youtube list value: ",""+aList.get(position));
                      youTubePlayer.cueVideo(aList.get(position));
                        }
                    @Override
                    public void onInitializationFailure(YouTubePlayer.Provider provider,
                                                        YouTubeInitializationResult youTubeInitializationResult) {

                    }
                });


            return view;
      }

Log Value:

Youtube Position:: 0

Youtube list value:: KSGYVl4ZgRs

Youtube Position:: 1

Youtube list value:: nCgQDjiotG0


UPDATE 2:

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.

UPDATE 3:

After lot of searching and @azizbekian soluion, I have resolved my problem. by using following solution.

 @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("Youtube Position: ", "" + position);
                                Log.e("Youtube list value: ", "" + 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;
    }

For getting youtube video thumnail, you can go throw.

How do I get a YouTube video thumbnail from the YouTube API?

like image 282
rockstar Avatar asked Aug 04 '17 17:08

rockstar


Video Answer


1 Answers

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

The problem is, that you are trying to inflate multiple fragments with same id, that's not possible. So, instead of adding YouTubePlayerFragment into the xml file, create it in the java code. Thus, custom_frag_youtube_list.xml will change to following:

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

Then in your adapter:

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    ...

    View view;
    YouTubePlayerFragment youtubeFragment;

    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();
        activity.getFragmentManager()
                .beginTransaction()
                .replace(id, youtubeFragment)
                .commit();
        view = temp;
        view.setTag(youtubeFragment);
    } else {
        view = convertView;
        youtubeFragment = view.getTag();
    }


    // interact with youtubeFragment
    ...

    return view;
}

Update

Judging by this thread, it seems like Google won't allow multiple youtube videos to be played simultaneously.

like image 78
azizbekian Avatar answered Nov 15 '22 15:11

azizbekian