Error inflating class com.google.android.youtube.player.YouTubePlayerView in ListView

为君一笑 提交于 2019-12-11 17:23:01

问题


I'm trying to get familiar with YoutubePlayerView and Mvvmcross model using Xamarin tool (for both Android, iOS)

I want to create a videolist, when users click into a video, it can be played in ListView

but I'm getting an error with Android

Binary XML file line #1: Error inflating class com.google.android.youtube.player.YouTubePlayerView

My code so far:

This is the List: VideoListView.axml:

<android.support.v4.widget.SwipeRefreshLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        local:MvxBind="Refresh RefreshCommand; Refreshing IsRefreshing">

        <dc.CustomMvxListView
            android:id="@+id/videoList"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    </android.support.v4.widget.SwipeRefreshLayout>

VideoListItemView.axml:

<dc.AlphaLinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:local="http://schemas.android.com/apk/res-auto"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    local:MvxBind="Click ItemClickCommand">

    <!--Video Thumbnail-->
    <FrameLayout
        android:layout_width="@dimen/ProductImageWidth"
        android:layout_height="@dimen/ProductImageHeight"
        android:layout_margin="@dimen/ExtraNewsImagePadding">
        <com.google.android.youtube.player.YouTubePlayerView
        android:id="@+id/playerView"
        android:layout_width="@dimen/ProductImageWidth"
        android:layout_height="@dimen/ProductImageHeight"
        android:layout_margin="@dimen/ExtraNewsImagePadding"/>
    </FrameLayout>

    <!--Video information-->
    <LinearLayout
    ....
    </LinearLayout>

</dc.AlphaLinearLayout>

VideoListView.cs

   public class VideoListView : DetailViewWithShare
    {
        protected override int LayoutId => Resource.Layout.VideoListView;
        private CustomMvxListView _videoListview;

        protected override void InitView(View view)
        {
            _videoListview = view.FindViewById<CustomMvxListView>(Resource.Id.videoList);
            _videoListview.ItemTemplateId = Resource.Layout.VideoListItemView;
        }

        protected override void CreateBinding()
        {
            var bindingSet = this.CreateBindingSet<VideoListView, VideoListViewModel>();

            bindingSet.Bind(_videoListview.Adapter)
                .For(v => v.ItemsSource)
                .To(vm => vm.VideoItemViewModels);

            bindingSet.Bind(_videoListview)
                .For(v => v.ScrollToBottom)
                .To(vm => vm.ScrollToBottomCommand);

            bindingSet.Apply();
        }
}

VideoListItemView.cs

namespace Droid.Views.Video
{
    public class VideoListItemView : YouTubeFailureRecoveryActivity
    {
        private string _youtubeId;
        public string YoutubeId
        {
            get
            {
                return _youtubeId;
            }
            set
            {
                _youtubeId = value;
                _player.CueVideo(value);
            }
        }

        private IYouTubePlayer _player;
        protected override IYouTubePlayerProvider YouTubePlayerProvider => FindViewById<YouTubePlayerView>(Resource.Id.playerView);

        protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);
            SetContentView(Resource.Layout.VideoListItemView);

            var youtubeView = FindViewById<YouTubePlayerView>(Resource.Id.playerView);
            youtubeView.Initialize(DEVELOPER_KEY, this);

        }

        public override void OnInitializationSuccess(IYouTubePlayerProvider provider, IYouTubePlayer player, bool wasRestored)
        {
            if (!wasRestored)
            {
                _player = player;
            }
        }
    }

    public abstract class YouTubeFailureRecoveryActivity : YouTubeBaseActivity, IYouTubePlayerOnInitializedListener
    {
        public const int RecoveryDialogRequest = 1;
        public string DEVELOPER_KEY = "AIzaSyDQjcaioPiOuMCPcugUmUxTijoy3sTLXkM";

        public void OnInitializationFailure(IYouTubePlayerProvider provider, YouTubeInitializationResult errorReason)
        {
            if (errorReason.IsUserRecoverableError)
            {
                errorReason.GetErrorDialog(this, RecoveryDialogRequest).Show();
            }
            else
            {
                var errorMessage = string.Format("Error initializing YouTube player", errorReason.ToString());
                Toast.MakeText(this, errorMessage, ToastLength.Long).Show();
            }
        }

        protected override void OnActivityResult(int requestCode, Result resultCode, Intent data)
        {
            if (requestCode == RecoveryDialogRequest)
            {
                // Retry initialization if user performed a recovery action
                YouTubePlayerProvider.Initialize(DEVELOPER_KEY, this);
            }
        }

        public abstract void OnInitializationSuccess(IYouTubePlayerProvider provider, IYouTubePlayer player, bool wasRestored);

        protected abstract IYouTubePlayerProvider YouTubePlayerProvider { get; }
    }
}

**Cannot implement YouTubeBaseActivity in ViewModel cause its ViewModel is using for both iOS and Android.

All I need is inflating YoutubePlayer for Android successfully first then figure the rest later.**

Done a lot of researching but I haven't solved this error yet: Binary XML file line #1: Error inflating class com.google.android.youtube.player.YouTubePlayerView

Help!!!!

来源:https://stackoverflow.com/questions/54778288/error-inflating-class-com-google-android-youtube-player-youtubeplayerview-in-lis

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