youtubeplayer 的 RecyclerView 上的 OnClickItem 出现异常

Posted

技术标签:

【中文标题】youtubeplayer 的 RecyclerView 上的 OnClickItem 出现异常【英文标题】:OnClickItem on RecyclerView for youtubeplayer is giving exception 【发布时间】:2021-01-31 06:32:26 【问题描述】:

我的片段显示了一个 youtube 播放器和回收器视图,其中包含来自 JSON 数据的视频列表。现在我正在尝试应用 onclick 但它不起作用。我试过 OnClickListener 但是当点击视频时,应用程序崩溃了。片段代码如下:

public class Fragment2 extends Fragment 

    private List<VideoYT> videoList = new ArrayList<>();
    private LinearLayoutManager manager;
    private AdapterHome1 adapter;
    private String nextPageToken = "";
    YouTubePlayer.OnInitializedListener mOnInitializedListener;
    private YouTubePlayer youTubePlayer;
    private YouTubePlayerSupportFragmentX youTubePlayerFragment;
    private RecyclerView rv;
    YouTubePlayerView mYouTubePlayerView;

    public Fragment2() 
    

    @Override
    public    View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) 
        View view = inflater.inflate(R.layout.fragment_1, container, false);
        // Inflate the layout for this fragment
        //Youtube Fragment

        youTubePlayerFragment = YouTubePlayerSupportFragmentX.newInstance();
        //youTubePlayerFragment.initialize(Constants.DEVELOPER_KEY, new YouTubePlayer.OnInitializedListener() 
        mOnInitializedListener = new YouTubePlayer.OnInitializedListener() 

            @Override
            public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer player,
                                                boolean wasRestored) 
                if (!wasRestored) 
                    youTubePlayer = player;

                    //set the player style default
                    youTubePlayer.setPlayerStyle(YouTubePlayer.PlayerStyle.DEFAULT);

                    //cue the 1st video by default
                    youTubePlayer.loadVideo(videoList.toString(),0);

                    youTubePlayer.addFullscreenControlFlag(YouTubePlayer.FULLSCREEN_FLAG_ALWAYS_FULLSCREEN_IN_LANDSCAPE);
                
            

            @Override
            public void onInitializationFailure(YouTubePlayer.Provider arg0, YouTubeInitializationResult arg1) 

                //print or show error if initialization failed
                Log.e(TAG, "Youtube Player View initialization failed");
            
        ;
        FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
        transaction.add(R.id.youtube_layout, youTubePlayerFragment).commit();
       

 //RecyclerView
        rv = view.findViewById(R.id.recyclerView);
 adapter = new AdapterHome1(getContext(),videoList,mOnInitializedListener,mYouTubePlayerView);

        manager = new LinearLayoutManager(getContext());
        rv.setAdapter(adapter);
        rv.setLayoutManager(manager);

           
        loadJSON();
      
        return view;
    
    private
    void loadJSON() 
       ...
    

而AdapterHome1代码如下;

public class AdapterHome1 extends RecyclerView.Adapter<RecyclerView.ViewHolder> 

    private Context context;
    private List<VideoYT> videoList;
    private int selectedPosition = 0;
   YouTubePlayer.OnInitializedListener mOnInitializedListener;
   YouTubePlayerView mYouTubePlayerView;


   public    AdapterHome1(Context context, List<VideoYT> videoList, YouTubePlayer.OnInitializedListener mOnInitializedListener, YouTubePlayerView mYouTubePlayerView) 
        this.context = context;
        this.videoList = videoList;
        this.mOnInitializedListener = mOnInitializedListener;
        this.mYouTubePlayerView = mYouTubePlayerView;
    

    public
    void setSelectedPosition(int position) 
        this.selectedPosition = selectedPosition;
        //when item selected notify the adapter
        notifyDataSetChanged();
    

    class YoutubeHolder extends RecyclerView.ViewHolder 

        ImageView thumbnail;
        TextView judul, tanggal;

        public YoutubeHolder(@NonNull View itemView) 
            super(itemView);
            thumbnail = itemView.findViewById(R.id.iv_thumbnail);
            judul = itemView.findViewById(R.id.tv_judul);
            tanggal = itemView.findViewById(R.id.tv_tglUpdate);
        

        public void setData(final VideoYT data) 
            final String getJudul = data.getSnippet().getTitle();
            String getTgl = data.getSnippet().getPublishedAt();
            String getThumb = data.getSnippet().getThumbnails().getMedium().getUrl();

            itemView.setOnClickListener(new View.OnClickListener() 
                @Override
                public void onClick(View v) 


                    mYouTubePlayerView.initialize(Constants.DEVELOPER_KEY, mOnInitializedListener);
                
            );

            judul.setText(getJudul);
            tanggal.setText(ChangeTo.getTimeAgo(getTgl));
            Picasso.get()
                    .load(getThumb)
                    .placeholder(R.mipmap.ic_launcher)
                    .fit()
                    .centerCrop()
                    .into(thumbnail, new Callback() 
                        @Override
                        public void onSuccess() 
                            Log.d(TAG, "Thumbnail berhasil ditampilkan");
                        

                        @Override
                        public void onError(Exception e) 
                            Log.e(TAG, "Thumbnail error: ", e);
                        
                    );
        
    

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) 
        LayoutInflater inflater = LayoutInflater.from(context);
        View view = inflater.inflate(R.layout.row_item_home, parent, false);
        return new YoutubeHolder(view);
    

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) 
        VideoYT videoYT = videoList.get(position);
        YoutubeHolder yth = (YoutubeHolder) holder;
        yth.setData(videoYT);
    

    @Override
    public int getItemCount() 
        return videoList.size();
    

当点击该项目时,应用程序因 logcat 错误而崩溃

 java.lang.NullPointerException: Attempt to invoke virtual method 'void com.google.android.youtube.player.YouTubePlayerView.initialize(java.lang.String, com.google.android.youtube.player.YouTubePlayer$OnInitializedListener)' on a null object reference
        at adapter.AdapterHome1$YoutubeHolder$1.onClick(AdapterHome1.java:79)
        at android.view.View.performClick(View.java:6935)
        at android.view.View$PerformClick.run(View.java:26214)
        at android.os.Handler.handleCallback(Handler.java:790)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:164)
        at android.app.ActivityThread.main(ActivityThread.java:7025)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:441)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1408)

几天以来,我一直在用不同的方法处理这个敌人,例如应用 OnItemTouchListener,但它也不起作用。

【问题讨论】:

【参考方案1】:

这个错误说mYouTubePlayerView 为空,这是有道理的,因为我可以看到它没有在 AdapterHome1 类的任何地方初始化。 您应该更改适配器类的构造函数以将其作为参数。

【讨论】:

用改进的代码编辑了问题,但错误是一样的。也发布了错误 我在 Fragment2 类的任何地方都看不到 mYouTubePlayerView 初始化...您必须先初始化它,然后才能在 Adapter 构造函数中使用它 youtubeplayerview的初始化看不懂。这行不正确吗?` mOnInitializedListener= new YouTubePlayer.OnInitializedListener() ` nope .. 问题出在mYouTubePlayerView 本身我认为您缺少此行mYouTubePlayerView= (YouTubePlayerView) findViewById(R.id.player); 或类似内容,请查看此链接guides.codepath.com/android/… 引用用于活动。我正在片段中工作。为了完成以下工作,我实现了` FragmentTransaction transaction = getChildFragmentManager().beginTransaction(); transaction.add(R.id.youtube_layout, youTubePlayerFragment).commit();`

以上是关于youtubeplayer 的 RecyclerView 上的 OnClickItem 出现异常的主要内容,如果未能解决你的问题,请参考以下文章

youtubeplayer 的 RecyclerView 上的 OnClickItem 出现异常

swift YoutubePlayer.swift

Android YouTubePlayer 在播放器顶部带有未经授权的覆盖

YouTubePlayer有时会在某些设备上暂停1秒后暂停

将 MPMoviePlayerController 的视图添加到 UIView

android - 无法加载多个YouTube视频