【发布时间】:2021-01-11 11:58:04
【问题描述】:
我正在使用 YouTube API 在我的应用中播放视频。视频 ID 是从 Firebase 数据库加载的。
这是数据库结构:
这是适配器 ViewHolder 的代码:
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);
这是我的清单:
<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>
每当我点击播放视频时,就会出现这个错误:
java.lang.ClassCastException: com.devapps.FirebaseOffline cannot be cast to android.app.Activity.
【问题讨论】:
-
这是因为上下文错误。检查 createVideoIntent 方法究竟需要什么。
-
它需要Activity作为第一个参数
-
使用 getApplicationContext() 或者这个。而不是(活动)上下文
-
它在适配器类中,而不是在活动中
标签: android firebase-realtime-database android-activity youtube-api