我正在使用我的自定义Adapter中的复选框。我使用活动中的接口在另一个列表视图中发送该复选框文本我面临的问题当我将项目添加到复选框并选中复选框时,应用程序停止工作..但当我重新打开应用程序并尝试再次检查它的workfine.when我尝试添加一个新项目复选框同样的问题发生,反之亦然
Logcat错误:
java.lang.NullPointerException: Attempt to invoke interface method 'void com.advoco.notepad.ListAdapter$onItemChecked.onItemClick(int)' on a null object reference
at com.advoco.notepad.ListAdapter$1.onCheckedChanged(ListAdapter.java:51)
at android.widget.CompoundButton.setChecked(CompoundButton.java:156)
at android.widget.CompoundButton.toggle(CompoundButton.java:115)
at android.widget.CompoundButton.performClick(CompoundButton.java:120)
at android.view.View$PerformClick.run(View.java:22429)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
public class ListAdapter extends ArrayAdapter<NoteDone> {
Context context;
LayoutInflater inflater;
List<NoteDone> noteDones;
private onItemChecked onClick;
public ListAdapter(Context context,int resource,List<NoteDone> noteDones) {
super(context, resource,noteDones);
this.context = context;
this.noteDones = noteDones;
inflater = LayoutInflater.from(context);
}
private class ViewHolder {
TextView notDone;
CheckBox checkBox;
}
public interface onItemChecked{
void onItemClick(int position);
}
public View getView(final int position, View view, ViewGroup parent) {
final ListAdapter.ViewHolder holder;
final NoteDone model = noteDones.get(position);
if (view == null) {
holder = new ListAdapter.ViewHolder();
view = inflater.inflate(R.layout.notdone, null);
holder.checkBox = (CheckBox) view.findViewById(R.id.checkBox);
view.setTag(holder);
} else {
holder = (ListAdapter.ViewHolder) view.getTag();
}
holder.checkBox.setText(model.getTask());
holder.checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
onClick.onItemClick(position);
}
});
return view;
}
public void setOnClick(onItemChecked onClick)
{
this.onClick=onClick;
}
}
我的活动课程:
public class ToDo extends AppCompatActivity implements
ListAdapter.onItemChecked{
DBhelper dBhelper;
ListView lstTask,listView;
List<NoteDone> taskList,tasklist2;
ListAdapter listAdapter;
ListAdapter2 listAdapter2;
SQLiteDatabase sqLiteDatabase;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.todo);
lstTask = (ListView)findViewById(R.id.nd);
listView = (ListView)findViewById(R.id.done);
LoadTask();
LoadTask2();
listAdapter.setOnClick(this);
/*lstTask.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String name = taskList.get(position).getTask();
dBhelper.insertNewTask2(name);
dBhelper.deleteTask(name);
LoadTask();
LoadTask2();
}
});*/
}
public void LoadTask() {
dBhelper = new DBhelper(getApplicationContext());
sqLiteDatabase = dBhelper.getReadableDatabase();
taskList = dBhelper.getTaskList(sqLiteDatabase);
if (taskList != null) {
listAdapter = new ListAdapter(getApplicationContext(), R.layout.notdone, taskList);
lstTask.setAdapter(listAdapter);
}
}
public void LoadTask2() {
dBhelper = new DBhelper(getApplicationContext());
sqLiteDatabase = dBhelper.getReadableDatabase();
tasklist2 = dBhelper.getTaskList2(sqLiteDatabase);
if (tasklist2 != null) {
listAdapter2 = new ListAdapter2(getApplicationContext(), R.layout.done, tasklist2);
listView.setAdapter(listAdapter2);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.todo_menu, menu);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()){
case R.id.action_add_task:
final EditText taskEditText = new EditText(this);
AlertDialog dialog = new AlertDialog.Builder(this)
.setTitle("Add New Task")
.setMessage("What do you want to do next?")
.setView(taskEditText)
.setPositiveButton("Add", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
String task = String.valueOf(taskEditText.getText());
dBhelper.insertNewTask(task);
LoadTask();
}
})
.setNegativeButton("Cancel",null)
.create();
dialog.show();
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
public void onItemClick(int position) {
String name = taskList.get(position).getTask();
dBhelper.insertNewTask2(name);
dBhelper.deleteTask(name);
LoadTask();
LoadTask2();
}
}
答案 0 :(得分:0)
问题在于您的发炎视图。
改变inflater.inflate(R.layout.notdone, null);
到
inflater.inflate(R.layout.notdone, parent, false);
希望这能解决问题
答案 1 :(得分:0)
listAdapter.setOnClick(this);
在设置适配器后,此行应包含在LoadTask()方法中。去掉 listAdapter.setOnClick(本);来自onCreate()。在LoadTask()上添加它。
lstTask.setAdapter(listAdapter);
listAdapter.setOnClick(this);