它继续弹出。我已经检查了我的语法。救命! 我的代码是这样的,他们点击编辑按钮,弹出一个对话框,然后更新我的SQLite数据库。
这是我的观点持有人
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
TextView foodTextView;
TextView calorieTextView;
Button editButton;
Button deleteButton;
// each data item is just a string in this case
public ViewHolder(View v) {
super(v);
foodTextView = (TextView) v.findViewById(R.id.food_text_view);
calorieTextView = (TextView) v.findViewById(R.id.calorie_text_view);
editButton = (Button) v.findViewById(R.id.edit_button);
deleteButton = (Button) v.findViewById(R.id.delete_button);
editButton.setOnClickListener(this);
deleteButton.setOnClickListener(this);
}
@Override
public void onClick(View view) {
if(view.getId()==R.id.edit_button){
itemClickCallback.onEditClick(this.getAdapterPosition());
} else if(view.getId()==R.id.delete_button){
itemClickCallback.onDeleteClick(this.getAdapterPosition());
}
}
}
以下是界面的实施
public void onEditClick(int p) {
foodDatabaseAdapter.helper.getWritableDatabase();
FragmentManager manager = getSupportFragmentManager();
EditItemDialog editItemDialog = new EditItemDialog();
editItemDialog.show(manager,"EditItemDialog");
}
我尝试过getLayoutPosition()
之类的其他内容,但它做同样的事情。虽然我记不起我所做的更改,但我已经对它进行了一些编辑,但它似乎只在我每次运行应用程序时工作一次,但下次尝试编辑时会崩溃。它在第一次编辑后也不会更新。救命啊!
这里是错误日志
06-22 10:27:18.624 25685-25685/com.leviebora.simplecaloriecounter E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.leviebora.simplecaloriecounter, PID: 25685
java.lang.NullPointerException: Attempt to invoke interface method 'void com.leviebora.simplecaloriecounter.MyAdapter$ItemClickCallback.onEditClick(int)' on a null object reference
at com.leviebora.simplecaloriecounter.MyAdapter$ViewHolder.onClick(MyAdapter.java:54)
at android.view.View.performClick(View.java:5198)
at android.view.View$PerformClick.run(View.java:21147)
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:5417)
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)
ItemClickCallback在适配器类中。以下是设置它的代码。
private ItemClickCallback itemClickCallback;
public interface ItemClickCallback {
void onEditClick(int p);
void onDeleteClick(int p);
}
public void setItemClickCallback(final ItemClickCallback itemClickCallback){
this.itemClickCallback = itemClickCallback;
}
这是onCreate
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
SimpleDateFormat currentDate = new SimpleDateFormat("ddMMyyyy");
Date todayDate = new Date();
thisDate = currentDate.format(todayDate);
foodDatabaseAdapter = new FoodDatabaseAdapter(this);
/* prefs = this.getSharedPreferences("runToday", MODE_PRIVATE);
edit = prefs.edit();
itemNumber = prefs.getInt("runToday",0);*/
recyclerView = (RecyclerView) findViewById(R.id.recyclerview);
adapter = new MyAdapter(this,getData());
recyclerView.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
recyclerView.addItemDecoration(new DividerItemDecoration(this));
recyclerView.setAdapter(adapter);
adapter.setItemClickCallback(this);
}
编辑我的适配器构造函数。这是解决方法。
public MyAdapter(Context context, List<Information> data, ItemClickCallback itemClickCallback ) {
inflater = LayoutInflater.from(context);
this.itemClickCallback = itemClickCallback;
this.data = data;
}
答案 0 :(得分:0)
在适配器上编写函数
public void setTtemClickCallback(itemClickCallback item) {
this.itemClickCallback = item;
}
并提供片段
adaptor.setTtemClickCallback(this);
希望这可能会有所帮助
答案 1 :(得分:0)
您必须将回调传递给视图持有者。例如,由构造函数:
private ItemClickCallback itemClickCallback;
public ViewHolder(View v, ItemClickCallback itemClickCallback) {
super(v);
this.itemClickCallback = itemClickCallback;
//more code
}
答案 2 :(得分:0)
请检查“itemClickCallback”界面未正确初始化..可能是
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
ItemClickCallback itemClickCallback;
public interface ItemClickCallback {
void onEditClick(int p);
void onDeleteClick(int p);
}
public ViewHolder(View v, ItemClickCallback itemClickCallback) {
super(v);
...........................
.............................
this.itemClickCallback = itemClickCallback;
}
}