ListView中的YouTubePlayerFragment与AppCompatActivity错误

时间:2017-08-04 17:41:25

标签: java android listview android-view android-youtube-api

我正在开发一个Android应用程序。在此应用程序中,我希望youtube视频播放在ListViewRecyclerViewAppCompatActivity的youtube播放器中。我使用了YouTubeBaseActivity它运行良好,但我需要AppCompatActivity。所以我使用YouTubePlayerFragmentYouTubePlayerSupportFragment来解决我的问题。

当我YouTubePlayerFragment使用AppCompatActivity时,它运行良好。但是我想在ListView中使用它,但是我遇到了错误。

 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>

所以,请建议我哪里错了?如何解决我的问题?

提前致谢。

更新1:

我正在使用@azizbekian解决方案。这是解决我的问题膨胀异常,但我有相同的多个布局的视频。

@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;
      }

记录值:

  

Youtube Position :: 0

     

Youtube列表值:: KSGYVl4ZgRs

     

Youtube Position :: 1

     

Youtube列表值:: nCgQDjiotG0

更新2:

我已经解决了所有问题,而不是一个问题。如果我在播放一个视频后播放视频时,适配器不刷新。所以我无法一次处理视频播放。

更新3:

经过大量的搜索和@azizbekian soluion,我解决了我的问题。通过使用以下解决方案。

 @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;
    }

要获得youtube视频节目,你可以去投票。

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

1 个答案:

答案 0 :(得分:8)

  

引起:java.lang.IllegalArgumentException:二进制XML文件行#8:复制id 0x7f0b0063,标记为null或父ID为0x7f0b0062,另一个片段为com.google.andout.youtube.player.YouTubePlayerFragment

问题是,你试图用相同的id来夸大多个片段,这是不可能的。因此,不是将YouTubePlayerFragment添加到xml文件中,而是在java代码中创建它。因此,custom_frag_youtube_list.xml将更改为以下内容:

<?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" />

然后在你的适配器中:

@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;
}

更新

根据this thread判断,Google似乎不允许同时播放多个YouTube视频。