我尝试使用 Swip To Dismiss RecyclerView ,我使用了这个tutorial
但是当我试图通过FragmentOne
方法删除swipeToDelete
中的项目时,会显示Error
:
java.lang.NullPointerException: Attempt to invoke virtual method 'java.util.UUID com.drgnme.listhamrah.Detail.getId()' on a null object reference
at com.drgnme.listhamrah.DetailLab.deleteDetail(DetailLab.java:82)
at com.drgnme.listhamrah.FragmentOne$DetailAdapter.swipeToDelete(FragmentOne.java:396)
at com.drgnme.listhamrah.ItemSwipeHelper.onSwiped(ItemSwipeHelper.java:43)
at android.support.v7.widget.helper.ItemTouchHelper$4.run(ItemTouchHelper.java:681)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5451)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
我在this
Fragment
项目的另一个地方Delete
使用this
并且工作得很好,不知道问题是什么这里..
DetailLab.get(getActivity()).deleteDetail(mDetail);
updateUI();
updateNumbers();
:
FragmentOne
这是我的public class FragmentOne extends Fragment {
private static final String
SAVED_NUMBER_VISIBLE = "number";
private RecyclerView mDetailRecyclerView;
private DetailAdapter mAdapter;
private boolean mNumberVisible;
private View view;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
}
@Override
public View onCreateView(LayoutInflater inflater,
ViewGroup container,
Bundle savedInstanceState) {
view = inflater.inflate(R.layout.fragment_one_layout,
container, false);
mDetailRecyclerView = (RecyclerView) view.findViewById(R.id.detail_recycler_view);
LinearLayoutManager layoutManager = new LinearLayoutManager(getContext());
layoutManager.setReverseLayout(true); //This will reverse the data order but not scroll the RecyclerView to the last item
layoutManager.setStackFromEnd(true); //For keeping data order same and simply scrolling the RecyclerView to the last item
mDetailRecyclerView.setLayoutManager(layoutManager);
if (savedInstanceState != null) {
mNumberVisible =
savedInstanceState.getBoolean(SAVED_NUMBER_VISIBLE);
}
updateUI();
return view;
}
@Override
public void onResume() {
super.onResume();
updateUI();
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putBoolean(SAVED_NUMBER_VISIBLE, mNumberVisible);
}
..
..
protected void updateUI() {
DetailLab detailLab = DetailLab.get(getActivity());
List<Detail> details = detailLab.getDetails();
if (details.size() == 0) {
LinearLayout linearLayout = (LinearLayout) view.findViewById(R.id.empty_view);
linearLayout.setVisibility(View.VISIBLE);
} else {
LinearLayout linearLayout = (LinearLayout) view.findViewById(R.id.empty_view);
linearLayout.setVisibility(View.GONE);
}
if (mAdapter == null) {
mAdapter = new DetailAdapter(details);
mDetailRecyclerView.setAdapter(mAdapter);
} else {
mAdapter.setDetails(details);
mAdapter.notifyDataSetChanged();
}
updateNumbers();
}
private class DetailHolder extends RecyclerView.ViewHolder
implements View.OnClickListener, View.OnLongClickListener {
private TextView mTitleTextView;
// private TextView mDateTextView;
private Detail mDetail;
private RatingBar mRatingBar;
public DetailHolder(LayoutInflater inflater, ViewGroup parent) {
super(inflater.inflate(R.layout.list_item_detail,
parent, false));
itemView.setOnClickListener(this);
itemView.setOnLongClickListener(this);
mTitleTextView = (TextView) itemView.findViewById(R.id.detail_title);
mRatingBar = (RatingBar) itemView.findViewById(R.id.ratingBar);
}
public void bind(Detail detail) {
mDetail = detail;
mTitleTextView.setText(mDetail.getTitle());
mRatingBar.setRating(mDetail.getRate());
}
@Override
public void onClick(View view) {
Intent intent = DetailPagerActivity.newIntent(getActivity(),
mDetail.getId());
startActivity(intent);
}
@Override
public boolean onLongClick(View v) {
AlertDialog.Builder alert = new AlertDialog.Builder(
getActivity());
alert.setMessage(R.string.alert);
alert.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
DetailLab.get(getActivity()).deleteDetail(mDetail);
updateUI();
updateNumbers();
dialog.dismiss();
}
});
alert.setNegativeButton("No", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
alert.show();
return true;
}
}
private class DetailAdapter extends RecyclerView.Adapter<DetailHolder> implements ItemTouchHelperAdapter {
private List<Detail> mDetails;
private Detail mDetail;
public DetailAdapter(List<Detail> details) {
mDetails = details;
}
@Override
public DetailHolder onCreateViewHolder(ViewGroup parent, int viewType) {
LayoutInflater layoutInflater =
LayoutInflater.from(getActivity());
return new DetailHolder(layoutInflater, parent);
}
@Override
public void onBindViewHolder(DetailHolder holder, int position) {
Detail detail = mDetails.get(position);
holder.bind(detail);
}
@Override
public int getItemCount() {
return mDetails.size();
}
public void setDetails(final List<Detail> details) {
mDetails = details;
ItemTouchHelper.Callback itemTouchCallBack=new ItemSwipeHelper(Direction.RIGHT, mAdapter);
ItemTouchHelper swipeToDismissTouchHelper =new ItemTouchHelper(itemTouchCallBack);
swipeToDismissTouchHelper.attachToRecyclerView(mDetailRecyclerView);
}
@Override
public void swipeToDelete(int position) {
DetailLab.get(getActivity()).deleteDetail(mDetail);
updateUI();
updateNumbers();
}
}
}
:
{{1}}
答案 0 :(得分:0)
问题不在于我使用的Library
,我应该为{em>删除提供item's position
,
所以我只是在我的swipeToDelete
方法的第一个中添加了这一行并修复了问题:
mDetail = mDetails.get(position);