我有一个cardView适配器,它允许我显示数据库中的数据。我刚刚实现了一个滑动删除,但有时我有这个错误:
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.view.View.unFocus(android.view.View)' on a null object reference
指向我的活动中的这行代码mRecyclerView.removeViewAt (viewHolder.getAdapterPosition ());
我不明白为什么我会出现这个错误,特别是有时候它会很好但有时候不会......
我的活动
public class Resume_game_new extends AppCompatActivity {
private RecyclerView mRecyclerView;
private RecyclerView.Adapter mAdapter;
private RecyclerView.LayoutManager mLayoutManager;
List<Game> mDataset;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_resume_game_new);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
mRecyclerView = (RecyclerView) findViewById(R.id.my_recycler_view_resume_game);
// use this setting to improve performance if you know that changes
// in content do not change the layout size of the RecyclerView
mRecyclerView.setHasFixedSize(true);
// use a linear layout manager
mLayoutManager = new LinearLayoutManager(this);
mRecyclerView.setLayoutManager(mLayoutManager);
// specify an adapter (see also next example)
mDataset = Data.bdd.getGames();
mAdapter = new CardsViewAdapter(mDataset);
mRecyclerView.setAdapter(mAdapter);
//Swipe to Delete
ItemTouchHelper swipeToDismissTouchHelper = new ItemTouchHelper(new ItemTouchHelper.SimpleCallback(
ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT, ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT) {
@Override
public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) {
return false;
}
@Override
public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction)
{
// Do Stuff
TextView textView_id_game = (TextView) viewHolder.itemView.findViewById(R.id.textView16);
Data.bdd.remove_game(Integer.parseInt(textView_id_game.getText().toString()));
mDataset.remove(viewHolder.getAdapterPosition());
mRecyclerView.removeViewAt(viewHolder.getAdapterPosition());
mAdapter.notifyItemRemoved(viewHolder.getLayoutPosition());
mAdapter.notifyItemRangeChanged(viewHolder.getAdapterPosition(), mDataset.size());
mAdapter.notifyDataSetChanged();
}
});
swipeToDismissTouchHelper.attachToRecyclerView(mRecyclerView);
}
}
我的适配器(只有重要代码)
public class CardsViewAdapter extends RecyclerView.Adapter<CardsViewAdapter.ViewHolder> {
private List<Game> mDataset;
private int rotationAngle = 0;
// Provide a reference to the views for each data item
// Complex data items may need more than one view per item, and
// you provide access to all the views for a data item in a view holder
static class ViewHolder extends RecyclerView.ViewHolder {
// each data item is just a string in this case
private TextView textView_idGame;
private ImageView imageView_arrow;
private LinearLayout linearlayoutToExpands;
private ConstraintLayout coordinatorLayoutToExpands;
boolean isPopupVisible;
private CardView cardView;
ViewHolder(View v) {
super(v);
textView_idGame = (TextView) v.findViewById(R.id.textView16);
imageView_arrow = (ImageView) v.findViewById(R.id.item_description_game_more);
linearlayoutToExpands = (LinearLayout) v.findViewById(R.id.popup_layout);
isPopupVisible = false;
cardView = (CardView) v.findViewById(R.id.cardView);
coordinatorLayoutToExpands = (ConstraintLayout) v.findViewById(R.id.viewToExpands);
}
}
// Provide a suitable constructor (depends on the kind of dataset)
CardsViewAdapter(List<Game> myDataset) {
mDataset = myDataset;
}
// Create new views (invoked by the layout manager)
@Override
public CardsViewAdapter.ViewHolder onCreateViewHolder(ViewGroup parent,
int viewType) {
// create a new view
View v = LayoutInflater.from(parent.getContext())
.inflate(R.layout.cards_resume_game, parent, false);
// set the view's size, margins, paddings and layout parameters
//...
ViewHolder vh = new ViewHolder(v);
return vh;
}
// Replace the contents of a view (invoked by the layout manager)
@Override
public void onBindViewHolder(final ViewHolder holder, final int position) {
// - get element from your dataset at this position
// - replace the contents of the view with that element
final Game game = mDataset.get(position);
...
// Set the expanded or collapsed mode here
if(game.expanded) {
expandView(holder.coordinatorLayoutToExpands);
holder.imageView_arrow.setRotation(180);
}
else {
collapseView(holder.coordinatorLayoutToExpands);
holder.imageView_arrow.setRotation(0);
}
// Now set the onClickListener like this
holder.imageView_arrow.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Animate the imageView here
animateImageView(holder.imageView_arrow);
// Toggle the expanded attribute value
if(game.expanded) game.expanded = false;
else game.expanded = true;
//Now call notifyDataSetChanged to make the change to effect
refreshList(position);
}
});
}
// Extra functions inside your adapter class to improve readability
private void collapseView(View v) {
CardsAnimationHelper.collapse(v);
}
private void expandView(View v) {
CardsAnimationHelper.expand(v);
}
private void refreshList(final int position) {
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
notifyItemChanged(position);
}
}, animationDuration);
}
// Return the size of your dataset (invoked by the layout manager)
@Override
public int getItemCount() {
return mDataset.size();
}
}
答案 0 :(得分:4)
从onSwiped()方法中删除这些代码行
mRecyclerView.removeViewAt(viewHolder.getAdapterPosition());
mAdapter.notifyItemRemoved(viewHolder.getLayoutPosition());
mAdapter.notifyItemRangeChanged(viewHolder.getAdapterPosition(), mDataset.size());
并且只使用它:
mAdapter.notifyDataSetChanged();
另请注意,您正在使用&#34; viewHolder.getLayoutPosition()&#34;而不是&#34; viewHolder.getAdapterPosition()&#34;对于notifyItemRemoved。
还提供您的cardView支持库版本。
答案 1 :(得分:0)
public class EnumThing
{
private SomeConstant aConstant;
private enum SomeConstant {CONSTANT1, CONSTANT2, CONSTANT3};
public EnumThing(SomeConstant thisConstant)
{
this.aConstant = thisConstant;
}
// Methods