I have an Adapter class for a RecyclerView
, and I use it for more than one Activity (for more than one RecyclerView
).
I get to these Activities through a StartActivityForResult() intent, but when I get to the 'Clairvoyant' one, it crashes and it gives me this java.lang.NullPointerException, but I don't know why, as it says the error is here:
@Override
public int getItemCount() {
return mArrayCourses.size();
}
But this Class is common for all the Activities, and the first two display without any error.
Adapter
Class
Please note this Class was not design by me, you can find the Original version here.
package com.example.leonardo.scurcola;
// Imports Skipped
public class CoursesAdapter extends RecyclerView.Adapter<CoursesAdapter.CoursesViewHolder> {
public class CoursesViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
TextView name;
TextView card;
CoursesViewHolder(View itemView) {
super(itemView);
itemView.setOnClickListener(this);
name = (TextView) itemView.findViewById(R.id.textName);
card = (TextView) itemView.findViewById(R.id.textCard);
}
@Override
public void onClick(View v) {
// The user may not set a click listener for list items, in which case our listener
// will be null, so we need to check for this
if (mOnEntryClickListener != null) {
mOnEntryClickListener.onEntryClick(v, getLayoutPosition());
}
}
}
private ArrayList<Player> mArrayCourses;
public CoursesAdapter(ArrayList<Player> arrayCourses) {
mArrayCourses = arrayCourses;
}
@Override
public int getItemCount() {
return mArrayCourses.size();
}
@Override
public CoursesViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.two_line_list_item_horizontal, parent, false);
return new CoursesViewHolder(view);
}
@Override
public void onBindViewHolder(CoursesViewHolder holder, int position) {
Player player = mArrayCourses.get(position);
holder.name.setText(player.getName());
holder.card.setText(player.getCardName());
}
@Override
public void onAttachedToRecyclerView(RecyclerView recyclerView) {
super.onAttachedToRecyclerView(recyclerView);
}
private OnEntryClickListener mOnEntryClickListener;
public interface OnEntryClickListener {
void onEntryClick(View view, int position);
}
public void setOnEntryClickListener(OnEntryClickListener onEntryClickListener) {
mOnEntryClickListener = onEntryClickListener;
}
}
Activity
w/ RecyclerView
package com.example.leonardo.scurcola;
// Imports Skipped
public class ListPlayersClairvoyant extends Activity {
RecyclerView myList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list_players_clairvoyant);
// Get the players and remove the Clairvoyant
Intent intent = this.getIntent();
Bundle bundle = intent.getExtras();
ArrayList<Player> playersNoClairvoyant = (ArrayList<Player>) bundle.getSerializable("PLAYERS");
for(Player player : playersNoClairvoyant){
if(player.getCardName().equals("Clairvoyant")){
playersNoClairvoyant.remove(player);
}
}
myList = (RecyclerView) findViewById(R.id.playersNoClairvoyant);
myList.setLayoutManager(new LinearLayoutManager(this));
CoursesAdapter adapter = new CoursesAdapter(playersNoClairvoyant);
myList.setAdapter(adapter);
// RecyclerView with a click listener
CoursesAdapter clickAdapter = new CoursesAdapter(playersNoClairvoyant);
clickAdapter.setOnEntryClickListener(new CoursesAdapter.OnEntryClickListener() {
@Override
public void onEntryClick(View view, int position) {
// Stuff that will happen when a list item is clicked
Intent intent = new Intent();
intent.putExtra("pos", position);
setResult(Activity.RESULT_OK, intent);
finish();
}
});
myList.setAdapter(clickAdapter);
}
}
Edit:
The error was at this line, I didn't change correctly the Activity to launch, so in the Activity I launched there was no getter to get the players List but another one instead.
case 2:
clairvoyant(); // Clairvoyant's turn
/* --- Let him choose ---*/
Intent intent = new Intent(this, ERROR_HERE.class);
Bundle bundle = new Bundle();
bundle.putSerializable("PLAYERS", (Serializable) players);
intent.putExtras(bundle);
startActivityForResult(intent, REQUEST_CLAIRVOYANT);
/* --------------------- */
nightInsideCounter++;
break;