Welcome to 16892 Developer Community-Open, Learning,Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I am using YouTube API to play videos in my app. The video ID's are loaded from Firebase database.

This is the database structure:

enter image description here

This is the Adapter ViewHolder's code:

 class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
    protected RelativeLayout relativeLayoutOverYouTubeThumbnailView;
    TextView channel_name;
    YouTubeThumbnailView youTubeThumbnailView;
    public ImageView playButton;


    public ViewHolder(@NonNull View itemView) {
        super(itemView);
        channel_name = itemView.findViewById(R.id.channel_name);
        playButton = itemView.findViewById(R.id.btnYoutube_player);
        playButton.setOnClickListener(this);
        relativeLayoutOverYouTubeThumbnailView = itemView.findViewById(R.id.relativeLayout_over_youtube_thumbnail);
        youTubeThumbnailView = itemView.findViewById(R.id.youtube_thumbnail);


    }

    @Override
    public void onClick(View view) {
        String id = list.get(getAdapterPosition()).getVid_ID();

       // ERROR IN BELOW LINE

        Intent intent = YouTubeStandalonePlayer.createVideoIntent((Activity) context, YoutubeConfig.getApiKey(), id, 100,
                true,
                false);
        context.startActivity(intent);

This is my manifest:

  <application
    android:name=".FirebaseOffline"
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">

    <activity android:name=".VideoActivity"
        android:configChanges="orientation|screenSize">
        <intent-filter>
            <action android:name=".Video.VideoActivity" />

            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>

    </activity>

Whenever I click to play the video, this error occurs:

java.lang.ClassCastException: com.devapps.FirebaseOffline cannot be cast to android.app.Activity.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
680 views
Welcome To Ask or Share your Answers For Others

1 Answer

You need to pass activity to your RecyclerView Adapter then change this into activity. Example: In your activity (Where you call recyclerview)

adapter = new ExampleRecyclerAdapter(CurrentActivity.this, data);

In your RecyclerView Adapter:

Activity activity;
ExampleRecyclerAdapter(CurrentActivity activity, Data data) {
this.activity = activity;
}

then replace in your code this into activity

context.startActivity(YouTubeStandalonePlayer.createVideoIntent(activity, DEVELOPER_KEY, VIDEO_ID));

}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to 16892 Developer Community-Open, Learning and Share
...