Android: Error inflating class fragment , while using YouTubePlayerSupportFragment

假如想象 提交于 2019-12-11 06:36:16

问题


Error inflating class fragment , while using YouTubePlayerSupportFragment inside list adapter as list_item_row. Reference link

My 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();
            }

Error Log:

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)

Also i just found one question Link to solve issue But still not getting solution how can i do it.

I have Activity->Fragment1->listview->row-> YouTubePlayerSupportFragment

after reference link i changed code :

 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();

But it shows error found YouTubePlayerSupportFragment required android.app.Fragment. So i tried to use supportFragmentv4 but inside adapter i am not able to get it's object.

来源:https://stackoverflow.com/questions/38694028/android-error-inflating-class-fragment-while-using-youtubeplayersupportfragme

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!