Я пытаюсь использовать RecyclerView внутри фрагмента. Но как только я завершил код, контекст адаптера показывает ошибку.
В функции onCreateViewHolder()
, context
показывает ошибку:
from (android.content.Context) in layoutInflator cannot be applied to (com.practice.musicplayer.libraryfragment)
public class SongCardAdapter extends RecyclerView.Adapter<SongCardAdapter.viewHolder> {
private ArrayList<SongCardModel> list;
private LibraryFragment context;
public SongCardAdapter(ArrayList<SongCardModel> list, LibraryFragment context) {
this.list = list;
this.context = context;
}
@NonNull
@Override
public viewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(context).inflate(R.layout.libray_recycler_view_layout, parent, false);
return new viewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull viewHolder holder, int position) {
SongCardModel model = list.get(position);
holder.albumArt.setImageResource(model.getImage());
holder.songName.setText(model.getSongName());
}
@Override
public int getItemCount() {
//setting the size of the recyclerView to the size of the list
return list.size();
}
public class viewHolder extends RecyclerView.ViewHolder {
ImageView albumArt;
TextView songName;
public viewHolder(@NonNull View itemView) {
super(itemView);
albumArt = itemView.findViewById(R.id.album_art);
songName = itemView.findViewById(R.id.song_name);
}
}
}
Для Cra sh Logs
java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View androidx.recyclerview.widget.RecyclerView.findViewById(int)' on a null object reference
at com.practice.musicplayer.LibraryFragment.onCreateView(LibraryFragment.java:27)
и в строке 27 у меня есть этот код:
libraryRecyclerView = (RecyclerView)libraryRecyclerView.findViewById(R.id.library_recyclerview);
Код фрагмента библиотеки:
public class LibraryFragment extends Fragment {
private RecyclerView libraryRecyclerView;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
libraryRecyclerView = (RecyclerView) libraryRecyclerView.findViewById(R.id.library_recyclerview);
ArrayList<SongCardModel> list = new ArrayList<>();
list.add(new SongCardModel(R.drawable.roses_cover, "Roses"));
list.add(new SongCardModel(R.drawable.roses_cover, "Roses"));
list.add(new SongCardModel(R.drawable.roses_cover, "Roses"));
list.add(new SongCardModel(R.drawable.roses_cover, "Roses"));
list.add(new SongCardModel(R.drawable.roses_cover, "Roses"));
list.add(new SongCardModel(R.drawable.roses_cover, "Roses"));
list.add(new SongCardModel(R.drawable.roses_cover, "Roses"));
list.add(new SongCardModel(R.drawable.roses_cover, "Roses"));
list.add(new SongCardModel(R.drawable.roses_cover, "Roses"));
SongCardAdapter adapter = new SongCardAdapter(list, getActivity());
libraryRecyclerView.setAdapter(adapter);
// Inflate the layout for this fragment
return inflater.inflate(R.layout.library_fragment, container, false);
}
}
SongCardModel:
public class SongCardModel {
//change the data type of image to string when accessing the online images
int image;
String songName;
//constructor of the class
public SongCardModel(int image, String songName) {
this.image = image;
this.songName = songName;
}
//getters and setter
public int getImage() {
return image;
}
public void setImage(int image) {
this.image = image;
}
public String getSongName() {
return songName;
}
public void setSongName(String songName) {
this.songName = songName;
}
}