我有RecyclerView
的适配器类,我将它用于多个活动(多个RecyclerView
)。
我通过StartActivityForResult()意图进入这些活动,但当我到达'Clairvoyant'时,它会崩溃,它会给我这个java.lang.NullPointerException,但我不知道为什么,正如它所说的那样错误在这里:
@Override
public int getItemCount() {
return mArrayCourses.size();
}
但是这个类对于所有活动都是通用的,前两个显示没有任何错误。
Adapter
班级请注意,此课程不是我设计的,您可以找到原始版本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);
}
}
修改
错误发生在这一行,我没有正确更改要启动的Activity,因此在我启动的Activity中没有getter来获取玩家List而是另一个。
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;
答案 0 :(得分:0)
看起来你没有在任何地方设置mArrayCourses,所以调用它的大小会给你一个NullPointerException!尝试确保将其设置为某种东西。