Error while adding Youtube Player

Asked

Viewed 58 times

2

I’m making an application that lists youtube videos , I created a layout to inflate in the main Activity and put the player in it

<com.google.android.youtube.player.YouTubePlayer
    android:id="@+id/youtubeVideo"
    android:layout_width="match_parent"
    android:layout_height="300dp"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true" />

<TextView
    android:id="@+id/tvImageName"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentStart="true"
    android:layout_marginBottom="172dp"
    android:gravity="center"
    android:text="Image name" />

But when I execute returns the error

android.view.InflateException: Binary XML file line #9: Binary XML 
file line #9: Class is not a View 
com.google.android.youtube.player.YouTubePlayer
Caused by: android.view.InflateException: Binary XML file line #9: 
Class is not a View com.google.android.youtube.player.YouTubePlayer
Caused by: java.lang.ClassCastException: interface 
com.google.android.youtube.player.YouTubePlayer cannot be cast to 
android.view.View

Adapter:

  import android.annotation.SuppressLint;
  import android.app.Activity;
 import android.content.Context;
      import android.media.MediaPlayer;
     import android.net.Uri;
    import android.os.AsyncTask;
   import android.support.annotation.LayoutRes;
   import android.support.annotation.NonNull;
  import android.support.annotation.Nullable;
   import android.util.Log;
  import android.view.LayoutInflater;
  import android.view.View;
  import android.view.ViewGroup;
   import android.widget.ArrayAdapter;
   import android.widget.Button;
  import android.widget.ImageView;
   import android.widget.MediaController;
   import android.widget.ProgressBar;
  import android.widget.TextView;
  import android.widget.VideoView;

  import 
 com.google.android.youtube.player.YouTubeInitializationResult;
 import com.google.android.youtube.player.YouTubePlayer;
 import com.google.android.youtube.player.YouTubePlayerView;
  import com.paivadeveloper.olheiros.Model.VideoUpload;
  import com.paivadeveloper.olheiros.R;

import org.w3c.dom.Text;

import java.util.List;

public class VideoListAdapter extends ArrayAdapter<VideoUpload> {
private Activity context;
private int resource;
private List<VideoUpload> listImage;
private Boolean isPlaying = false;
private int current = 0;
private int duration = 0;
private ProgressBar currentProgress;
private VideoView img;
private TextView currentTime;
private YouTubePlayer youTubePlayerView;
YouTubePlayer.OnInitializedListener onInitializedListener;

   public VideoListAdapter(@NonNull Activity context, @LayoutRes int 
  resource, @NonNull List<VideoUpload> objects) {
    super(context, R.layout.image_item, objects);
    this.context = context;
    listImage = objects;
}

@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
    LayoutInflater inflater = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

     @SuppressLint("ViewHolder") View v = 
     inflater.inflate(R.layout.image_item, parent, false);


    onInitializedListener = new YouTubePlayer.OnInitializedListener() 
    {
        @Override
        public void onInitializationSuccess(YouTubePlayer.Provider 
     provider, YouTubePlayer youTubePlayer, boolean b) {

        }

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

        }
    };


    TextView tvName = v.findViewById(R.id.tvImageName);

    YouTubePlayerView youTubePlayerView = 
    (YouTubePlayerView)v.findViewById(R.id.youtube_player);

    String tvNameString;
    tvNameString = listImage.get(position).getName();


    tvName.setText(tvNameString);




    return v;
}

}

  • the error that is signaling is that with.google.android.youtube.player.Youtubeplayer is not a view, where Voce took this snippet ?

  • from what I understand you’re using wrong with.google.android.youtube.player.Youtubeplayer

2 answers

1

Is that what you want.

<com.google.android.youtube.player.YouTubePlayerView
android:id="@+id/youtube_player"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
  • Test, gave the same mistake =(

  • I can see your Radle?

  • implementation files('libs/Youtubeandroidplayerapi.jar')

  • are sure my answer does not solve, here it worked, you could clean the project and try again ?

  • 1

    when I used your xml I received the same ero, but with my reply it worked here

1


The problem was that my main activity was not with extends Youtubebasectivity

  • 1

    I still think about it ,I’m glad you solved.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.