viewholder onclickListener 空对象引用

viewholder onclickListener null object reference

尝试在空对象引用上调用接口方法'void com.example.imovie.adapter.MovieItemClickListener.onMovieClick(com.example.imovie.models.Movie, android.widget.ImageView)'

public class MovieAdapter extends RecyclerView.Adapter<MovieAdapter.MyViewHolder> {

    Context context ;
    List<Movie> mData;
    MovieItemClickListener movieItemClickListener;


    public MovieAdapter(Context context, List<Movie> mData, MovieItemClickListener listener) {
        this.context = context;
        this.mData = mData;
        movieItemClickListener = listener;
    }


    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {

        View view = LayoutInflater.from(context).inflate(R.layout.item_movie,viewGroup,false);
        return new MyViewHolder(view);

    }

    @Override
    public void onBindViewHolder(@NonNull MyViewHolder myViewHolder, int i) {


        myViewHolder.TvTitle.setText(mData.get(i).getTitle());
        myViewHolder.ImgMovie.setImageResource(mData.get(i).getThumbnail());


    }

    @Override
    public int getItemCount() {
        return mData.size();
    }



    public class MyViewHolder extends RecyclerView.ViewHolder {


        private TextView TvTitle;
        private ImageView ImgMovie;


        public MyViewHolder(@NonNull View itemView) {

            super(itemView);
            TvTitle = itemView.findViewById(R.id.item_movie_title);
            ImgMovie = itemView.findViewById(R.id.item_movie_img);


            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    movieItemClickListener.onMovieClick(mData.get(getAdapterPosition()),ImgMovie);//i have  a null object reference
                }
            });


        }
    }
}



public class HomeFragment extends Fragment implements MovieItemClickListener {

    private HomeViewModel homeViewModel;
    private List<Slide> listSlides;
    private ViewPager slidePager;
    private TabLayout indicator;
    private RecyclerView moviesRV;
    private RecyclerView moviesRV2;
    private MovieItemClickListener movieItemClickListener;


    public View onCreateView(@NonNull LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {

        homeViewModel =
                ViewModelProviders.of(this).get(HomeViewModel.class);
        View root = inflater.inflate(R.layout.fragment_home, container, false);

        slidePager = root.findViewById(R.id.slider_pager);
        indicator = root.findViewById(R.id.indicator);
        moviesRV = root.findViewById(R.id.rsViewMovies);
        moviesRV2 = root.findViewById(R.id.rsViewMovies2);
        iniSlider();

        final SliderPagerAdapter sliderPagerAdapteradapter = new SliderPagerAdapter(getContext(), listSlides);
        final MovieAdapter movieAdapter = new MovieAdapter(getContext(), DateSource.getPopularMovies(), movieItemClickListener);


        final Timer timer = new Timer();
        timer.scheduleAtFixedRate(new SliderTimer(), 4000, 6000);

        homeViewModel.getText().observe(this, new Observer<String>() {
            @Override
            public void onChanged(@Nullable String s) {

                slidePager.setAdapter(sliderPagerAdapteradapter);
                indicator.setupWithViewPager(slidePager, true);
                moviesRV.setAdapter(movieAdapter);
                moviesRV.setLayoutManager(new LinearLayoutManager(getContext(), LinearLayoutManager.HORIZONTAL, false));

                moviesRV2.setAdapter(movieAdapter);
                moviesRV2.setLayoutManager(new LinearLayoutManager(getContext(), LinearLayoutManager.HORIZONTAL, false));
            }
        });

        return root;
    }


    @Override
    public void onMovieClick(Movie movie, ImageView imageView) {

        Intent intent = new Intent(getContext(), MovieDetailActivity.class);


        intent.putExtra("title", movie.getTitle());
        intent.putExtra("imgURL", movie.getThumbnail());
        intent.putExtra("imgCover", movie.getCoverPhoto());


        ActivityOptions options = ActivityOptions.makeSceneTransitionAnimation(getActivity(),
                imageView, "sharedName");

        startActivity(intent, options.toBundle());


        Toast.makeText(getContext(), "item clicked : " + movie.getTitle(), Toast.LENGTH_LONG).show();

    }



    class SliderTimer extends TimerTask {


        @Override
        public void run() {

            if (getActivity() != null) {

                getActivity().runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        if (slidePager.getCurrentItem() < listSlides.size() - 1) {
                            slidePager.setCurrentItem(slidePager.getCurrentItem() + 1);
                        } else {
                            slidePager.setCurrentItem(0);
                        }
                    }
                });

            }
        }
    }


    private void iniSlider() {

        listSlides = new ArrayList<>();
        listSlides.add(new Slide(R.drawable.a, "slide title \n movie title"));
        listSlides.add(new Slide(R.drawable.b, "slide title"));
        listSlides.add(new Slide(R.drawable.a, "slide title \n movie title"));
        listSlides.add(new Slide(R.drawable.b, "slide title"));

    }


}

如果我没记错的话,HomeFragment.movieItemClickListener 永远不会被初始化。

您可能希望在 HomeFragment 中进行以下更改:

1。删除行

private MovieItemClickListener movieItemClickListener;

2。在这一行

final MovieAdapter movieAdapter = new MovieAdapter(getContext(), DateSource.getPopularMovies(), movieItemClickListener);

movieItemClickListener 更改为 this,因为 HomeFragment 实现了 MovieItemClickListener

这应该可以修复 NullPointerException。