Android:使用YouTubePlayerSupportFragment时出错类片段错误

时间:2016-08-01 08:19:52

标签: java android fragment android-youtube-api

在列表适配器中使用YouTubePlayerSupportFragment作为list_item_row时,错误地膨胀了类片段。 Reference link

我的list_item_row.xml

<RelativeLayout
    android:layout_width="match_parent"
    android:id="@+id/relativeLayout_youtube_player"
    android:layout_height="@dimen/preivew_image_height">


    <fragment
        android:name="com.google.android.youtube.player.YouTubePlayerSupportFragment"
        android:id="@+id/youtube_player_fragment"
        android:layout_width="match_parent"
        android:layout_height="@dimen/preivew_image_height"/>


    <ImageView
        android:id="@+id/button_youtube_player"
        android:layout_width="match_parent"
        android:layout_height="@dimen/preivew_image_height"
        android:scaleType="center"
        android:visibility="gone"
        android:contentDescription="@null"
        android:src="@drawable/play" />

</RelativeLayout>

Adapter.java:

if (convertView == null) {

                convertView = inflater.inflate(R.layout.list_item_feed_talk_url, parent, false);

                holder = new ViewHolder();

                holder.imageView_preview_image_box = (ImageView) convertView.findViewById(R.id.imageView_preview_image_box);
                holder.linearLayout_url_youtube = (LinearLayout) convertView.findViewById(R.id.linearLayout_url_youtube);
                holder.linearLayout_url = (LinearLayout) convertView.findViewById(R.id.linearLayout_url);
                holder.textView_box_title_yt = (TextView) convertView.findViewById(R.id.textView_box_title_yt);
                holder.textView_box_sub_title = (TextView) convertView.findViewById(R.id.textView_box_sub_title);
                holder.textView_box_sub_title_yt = (TextView) convertView.findViewById(R.id.textView_box_sub_title_yt);
                holder.relativeLayout_youtube_player = (RelativeLayout) convertView.findViewById(R.id.relativeLayout_youtube_player);

                convertView.setTag(holder);

            } else {

                holder = (ViewHolder) convertView.getTag();

            }

            feedListData = feedListDataArrayList.get(position);


            try {
                holder.linearLayout_url_youtube.setVisibility(View.VISIBLE);
                holder.linearLayout_url.setVisibility(View.GONE);
                Log.e("getUrl", ":" + Utility.getYoutubeVideoId(feedListData.getUrl()));

                holder.youtube_player_fragment = YouTubePlayerSupportFragment.newInstance();
                if (feedListData.getIsVideo()!=null && feedListData.getIsVideo().equals("1")) {
                    holder.youtube_player_fragment.initialize(activity.getResources().getString(R.string.YouTube_API_KEY), new YouTubePlayer.OnInitializedListener() {
                        @Override
                        public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer youTubePlayer, boolean b) {
                            if (null == youTubePlayer) return;
                            if (!b) {
                                youTubePlayer.setPlayerStyle(YouTubePlayer.PlayerStyle.DEFAULT);
                                Log.e("getUrl", ":" + Utility.getYoutubeVideoId(feedListData.getUrl()));
                                youTubePlayer.cuePlaylist(Utility.getYoutubeVideoId(feedListData.getUrl()));
                                //Save reference of initialized player in class level attribute

                            }
                        }

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

                        }
                    });



                    try {
                        if (feedListData.getUrlTitle() != null && feedListData.getUrlTitle().length() > 0) {
                            holder.textView_box_title_yt.setVisibility(View.VISIBLE);
                            holder.textView_box_title_yt.setText(feedListData.getUrlTitle());
                        } else {
                            holder.textView_box_title_yt.setVisibility(View.GONE);
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }

                    try {

                        if (feedListData.getUrlTitle() != null && feedListData.getUrlTitle().length() > 0) {
                            holder.textView_box_sub_title_yt.setVisibility(View.VISIBLE);
                            holder.textView_box_sub_title_yt.setText(feedListData.getWebsiteName());
                        } else {
                            holder.textView_box_sub_title_yt.setVisibility(View.GONE);
                        }

                    } catch (Exception e) {
                        e.printStackTrace();
                    }


                } else {
                    holder.linearLayout_url_youtube.setVisibility(View.GONE);
                    holder.linearLayout_url.setVisibility(View.VISIBLE);

                   }
            } catch (Exception e) {
                e.printStackTrace();
            }

错误日志:

FATAL EXCEPTION: main
                                                            Process: com.app.user, PID: 7617
                                                            android.view.InflateException: Binary XML file line #81: Binary XML file line #15: Error inflating class fragment
                                                                at android.view.LayoutInflater.inflate(LayoutInflater.java:539)
                                                                at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
                                                                at com.app.user.adapter.adapter.base.FeedListAdapter.getView(FeedListAdapter.java:1005)
                                                                at android.widget.HeaderViewListAdapter.getView(HeaderViewListAdapter.java:220)
                                                                at android.widget.AbsListView.obtainView(AbsListView.java:2346)
                                                                at android.widget.ListView.makeAndAddView(ListView.java:1876)
                                                                at android.widget.ListView.fillDown(ListView.java:702)
                                                                at android.widget.ListView.fillGap(ListView.java:666)
                                                                at android.widget.AbsListView.trackMotionScroll(AbsListView.java:5060)
                                                                at android.widget.AbsListView$FlingRunnable.run(AbsListView.java:4608)
                                                                at android.view.Choreographer$CallbackRecord.run(Choreographer.java:858)
                                                                at android.view.Choreographer.doCallbacks(Choreographer.java:670)
                                                                at android.view.Choreographer.doFrame(Choreographer.java:603)
                                                                at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:844)
                                                                at android.os.Handler.handleCallback(Handler.java:746)
                                                                at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                at android.os.Looper.loop(Looper.java:148)
                                                                at android.app.ActivityThread.main(ActivityThread.java:5443)
                                                                at java.lang.reflect.Method.invoke(Native Method)
                                                                at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728)
                                                                at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
                                                             Caused by: android.view.InflateException: Binary XML file line #15: Error inflating class fragment
                                                                at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:782)
                                                                at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
                                                                at android.view.LayoutInflater.rInflate(LayoutInflater.java:835)
                                                                at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:798)
                                                                at android.view.LayoutInflater.rInflate(LayoutInflater.java:838)
                                                                at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:798)
                                                                at android.view.LayoutInflater.parseInclude(LayoutInflater.java:971)
                                                                at android.view.LayoutInflater.rInflate(LayoutInflater.java:831)
                                                                at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:798)
                                                                at android.view.LayoutInflater.rInflate(LayoutInflater.java:838)
                                                                at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:798)
                                                                at android.view.LayoutInflater.rInflate(LayoutInflater.java:838)
                                                                at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:798)
                                                                at android.view.LayoutInflater.rInflate(LayoutInflater.java:838)
                                                                at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:798)
                                                                at android.view.LayoutInflater.inflate(LayoutInflater.java:515)
                                                                at android.view.LayoutInflater.inflate(LayoutInflater.java:423) 
                                                                at com.app.user.adapter.adapter.base.FeedListAdapter.getView(FeedListAdapter.java:1005) 
                                                                at android.widget.HeaderViewListAdapter.getView(HeaderViewListAdapter.java:220) 
                                                                at android.widget.AbsListView.obtainView(AbsListView.java:2346) 
                                                                at android.widget.ListView.makeAndAddView(ListView.java:1876) 
                                                                at android.widget.ListView.fillDown(ListView.java:702) 
                                                                at android.widget.ListView.fillGap(ListView.java:666) 
                                                                at android.widget.AbsListView.trackMotionScroll(AbsListView.java:5060) 
                                                                at android.widget.AbsListView$FlingRunnable.run(AbsListView.java:4608) 
                                                                at android.view.Choreographer$CallbackRecord.run(Choreographer.java:858) 
                                                                at android.view.Choreographer.doCallbacks(Choreographer.java:670) 
                                                                at android.view.Choreographer.doFrame(Choreographer.java:603) 
                                                                at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:844) 
                                                                at android.os.Handler.handleCallback(Handler.java:746) 
                                                                at android.os.Handler.dispatchMessage(Handler.java:95) 
                                                                at android.os.Looper.loop(Looper.java:148) 
                                                                at android.app.ActivityThread.main(ActivityThread.java:5443) 
                                                                at java.lang.reflect.Method.invoke(Native Method) 
                                                                at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728) 
                                                                at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618) 
                                                             Caused by: java.lang.IllegalArgumentException: Binary XML file line #15: Duplicate id 0x7f1001d0, tag null, or parent id 0x7f1001cf with another fragment for com.google.android.youtube.player.YouTubePlayerSupportFragment
                                                                at android.support.v4.app.FragmentManagerImpl.onCreateView(FragmentManager.java:2293)
                                                                at android.support.v4.app.FragmentController.onCreateView(FragmentController.java:120)
                                                                at android.support.v4.app.FragmentActivity.dispatchFragmentsOnCreateView(FragmentActivity.java:356)
                                                                at android.support.v4.app.BaseFragmentActivityHoneycomb.onCreateView(BaseFragmentActivityHoneycomb.java:31)
                                                                at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:79)
                                                                at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:754)
                                                                at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704) 
                                                                at android.view.LayoutInflater.rInflate(LayoutInflater.java:835) 
                                                                at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:798) 
                                                                at android.view.LayoutInflater.rInflate(LayoutInflater.java:838) 
                                                                at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:798) 
                                                                at android.view.LayoutInflater.parseInclude(LayoutInflater.java:971) 
                                                                at android.view.LayoutInflater.rInflate(LayoutInflater.java:831) 
                                                                at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:798) 
                                                                at android.view.LayoutInflater.rInflate(LayoutInflater.java:838) 
                                                                at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:798) 
                                                                at android.view.LayoutInflater.rInflate(LayoutInflater.java:838) 
                                                                at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:798) 
                                                                at android.view.LayoutInflater.rInflate(LayoutInflater.java:838) 
                                                                at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:798) 
                                                                at android.view.LayoutInflater.inflate(LayoutInflater.java:515) 
                                                                at android.view.LayoutInflater.inflate(LayoutInflater.java:423) 
                                                                at com.app.user.adapter.adapter.base.FeedListAdapter.getView(FeedListAdapter.java:1005) 
                                                                at android.widget.HeaderViewListAdapter.getView(HeaderViewListAdapter.java:220) 
                                                                at android.widget.AbsListView.obtainView(AbsListView.java:2346) 
                                                                at android.widget.ListView.makeAndAddView(ListView.java:1876) 
                                                                at android.widget.ListView.fillDown(ListView.java:702) 
                                                                at android.widget.ListView.fillGap(ListView.java:666) 
                                                                at android.widget.AbsListView.trackMotionScroll(AbsListView.java:5060) 
                                                                at android.widget.AbsListView$FlingRunnable.run(AbsListView.java:4608) 
                                                                at android.view.Choreographer$CallbackRecord.run(Choreographer.java:858) 
                                                                at android.view.Choreographer.doCallbacks(Choreographer.java:670) 
                                                                at android.view.Choreographer.doFrame(Choreographer.java:603) 
                                                                at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:844) 
                                                                at android.os.Handler.handleCallback(Handler.java:746) 
                                                                at android.os.Handler.dispatchMessage(Handler.java:95) 
                                                                at android.os.Looper.loop(Looper.java:148) 
                                                                at android.app.ActivityThread.main(ActivityThread.java:5443) 
                                                                at java.lang.reflect.Method.invoke(Native Method)

此外,我刚发现一个问题Link to solve issue但仍然没有得到解决方案我该怎么办呢。

我有Activity-&gt; Fragment1-&gt; listview-&gt; row-&gt; YouTubePlayerSupportFragment

在参考链接后我改变了代码:

 holder.youtube_player_fragment = new YouTubePlayerSupportFragment();

                if (feedListData.getIsVideo()!=null && feedListData.getIsVideo().equals("1")) {
                    holder.youtube_player_fragment.initialize(activity.getResources().getString(R.string.YouTube_API_KEY), new YouTubePlayer.OnInitializedListener() {
                        @Override
                        public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer youTubePlayer, boolean b) {
                            if (null == youTubePlayer) return;
                            if (!b) {
                                youTubePlayer.setPlayerStyle(YouTubePlayer.PlayerStyle.DEFAULT);
                                Log.e("getUrl", ":" + Utility.getYoutubeVideoId(feedListData.getUrl()));
                                youTubePlayer.cuePlaylist(Utility.getYoutubeVideoId(feedListData.getUrl()));


                            }
                        }

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

                        }
                    });

                    FragmentManager fragmentManager = activity.getFragmentManager();
                    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
                    fragmentTransaction.replace(R.id.youtube_player_frame, holder.youtube_player_fragment);
                    fragmentTransaction.commit();

但它显示错误,发现YouTubePlayerSupportFragment需要android.app.Fragment。所以我尝试使用supportFragmentv4,但在适配器内我无法得到它的对象。

0 个答案:

没有答案