This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12个答案)
3个月前关闭。
当我单击特定项目时,我试图将其删除。我已经运行了日志,所以我知道它正在选择一个项目,我只是不明白错误的含义。如果重要的话,我正在使用会议室数据库并使用回收站视图。
java.lang.NullPointerException:尝试调用虚拟方法'void
uk.edu.le.co2103.myrecipebook.RecipeListAdapter.notifyItemRemoved(int)'在空对象上
参考
在uk.edu.le.co2103.myrecipebook.MainActivity $ 4.onClick(MainActivity.java:126)
我检查了清单中是否有数据,因此下面是日志。
D /主要活动:列表:[uk.edu.le.co2103.myrecipebook.Recipe@c97e34d,uk.edu.le.co2103.myrecipebook.Recipe@e7b6e02,uk.edu.le.co2103.myrecipebook.Recipe@d9ea313]
2020-04-01 23:07:29.300 25679-25679 / uk.edu.le.co2103.myrecipebook D / MAIN活动:索引:2
这是主要活动中我删除部分的代码
我的适配器代码
}
(12个答案)
3个月前关闭。
当我单击特定项目时,我试图将其删除。我已经运行了日志,所以我知道它正在选择一个项目,我只是不明白错误的含义。如果重要的话,我正在使用会议室数据库并使用回收站视图。
java.lang.NullPointerException:尝试调用虚拟方法'void
uk.edu.le.co2103.myrecipebook.RecipeListAdapter.notifyItemRemoved(int)'在空对象上
参考
在uk.edu.le.co2103.myrecipebook.MainActivity $ 4.onClick(MainActivity.java:126)
我检查了清单中是否有数据,因此下面是日志。
D /主要活动:列表:[uk.edu.le.co2103.myrecipebook.Recipe@c97e34d,uk.edu.le.co2103.myrecipebook.Recipe@e7b6e02,uk.edu.le.co2103.myrecipebook.Recipe@d9ea313]
2020-04-01 23:07:29.300 25679-25679 / uk.edu.le.co2103.myrecipebook D / MAIN活动:索引:2
这是主要活动中我删除部分的代码
public void onItemClick(int position, View view) {
Log.d(TAG, "onItemClick Position: " + position);
AlertDialog.Builder alertDialog = new AlertDialog.Builder(this);
alertDialog.setTitle("Edit or Delete...");
alertDialog.setPositiveButton("Edit", new
DialogInterface.OnClickListener()
alertDialog.setNegativeButton("Delete", new
DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
//Delete
Log.d(TAG, "List: " + recipesList);
Log.d(TAG, "Index: " + position);
int removeIndex = position;
recipesList.remove(removeIndex);
mAdapter.notifyItemRemoved(removeIndex);
}
});
我的适配器代码
public class RecipeListAdapter extends
RecyclerView.Adapter<RecipeListAdapter.RecipeViewHolder> {
private OnItemClickListener mListener;
private LiveData<List<Recipe>> recipeList;
public interface OnItemClickListener{
void onItemClick(int position, View view);
}
public void setOnItemClickListener(OnItemClickListener listener){
mListener = listener;
}
class RecipeViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
private final TextView recipeItemView;
private RecipeViewHolder(View itemView) {
super(itemView);
recipeItemView = itemView.findViewById(R.id.textView);
itemView.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
if (mListener != null){
int position = getAdapterPosition();
if (position != RecyclerView.NO_POSITION){
mListener.onItemClick(position, v);
}
}
}
});
}
@Override
public void onClick(View view) {
mListener.onItemClick(getAdapterPosition(), view);
}
}
private final LayoutInflater mInflater;
private List<Recipe> mRecipes; // Cached copy of words
RecipeListAdapter(Context context) {
mInflater = LayoutInflater.from(context);
this.recipeList = recipeList;
}
@Override
public RecipeViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = mInflater.inflate(R.layout.recyclerview_item, parent, false);
return new RecipeViewHolder(itemView);
}
@Override
public void onBindViewHolder(RecipeViewHolder holder, int position) {
if (mRecipes != null) {
Recipe current = mRecipes.get(position);
holder.recipeItemView.setText(current.getName());
} else {
// Covers the case of data not being ready yet.
holder.recipeItemView.setText("No Recipes");
}
}
void setWords(List<Recipe> recipes){
mRecipes = recipes;
notifyDataSetChanged();
}
// getItemCount() is called many times, and when it is first called,
// mWords has not been updated (means initially, it's null, and we can't return null).
@Override
public int getItemCount() {
if (mRecipes != null)
return mRecipes.size();
else return 0;
}
public interface OnNoteListener{}
}
最佳答案
您可以在这里共享您的适配器代码吗?过去我遇到过这个问题,它的发生是因为您需要刷新适配器中的列表内容(您现在仅通过.notifyItemRemoved(int index)注意到它),但我确实需要查看您的适配器以帮助您了解如何做到这一点,在我看来,观察员是最好的方法。
09-26 14:12