我遇到了另一个问题。
我有一个部分可行的脚本。当ListView中的条目大约为5-10时,它可以工作。但是,当我在50-60左右添加50个条目时,它会在调用脚本时抛出java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.View.findViewById(int)' on a null object reference
。
这是onCreate,其中eraseButton从SQLite数据库中删除列表视图中的选定行。
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main3);
//SQLite Database
final DatabaseHandler db = new DatabaseHandler(this);
//listview
final ListView questionList = (ListView) findViewById(R.id.questionList);
final Cursor cursor = db.getData();
final SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.list_item, cursor, new String[] { DatabaseHandler.COL_QUESTION }, new int[] { R.id.checkedTextView1 },0);
assert questionList != null;
questionList.setAdapter(adapter);
questionList.setItemsCanFocus(false);
questionList.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
//Erasebutton listener
final Button eraseButton = (Button) findViewById(R.id.eraseButton);
assert eraseButton != null;
eraseButton.setOnClickListener(new View.OnClickListener() { //erasebutton onclick
public void onClick(View eraseButton) {
SparseBooleanArray checked = questionList.getCheckedItemPositions();
for (int i = questionList.getCount() - 1; i >= 0; i--) {
if (checked.get(i)) {
//What to do with selected listitems
TextView tv = (TextView) questionList.getChildAt(i).findViewById(R.id.checkedTextView1);
db.removeData(tv.getText().toString());
}
}
checked.clear();
Cursor newCursor = db.getData();
adapter.swapCursor(newCursor);
}
});
//addbutton listener
final Button addButton = (Button) findViewById(R.id.addButton);
assert addButton != null;
addButton.setOnClickListener(new View.OnClickListener() { //Nextbutton onclick
public void onClick(View addButton) {
EditText addText = (EditText) findViewById(R.id.addText);
assert addText != null;
String text = addText.getText().toString();
if(!text.matches("") && text.startsWith("Jag har aldrig ")) {
addText.setText("");
db.insertData(text);
Cursor newCursor = db.getData();
adapter.swapCursor(newCursor);
} else {
Toast.makeText(Main3Activity.this, "Börja din fråga med \"Jag har aldrig\"", Toast.LENGTH_SHORT).show();
}
}
});
//Nextbutton listener
final Button returnButton = (Button) findViewById(R.id.returnButton);
assert returnButton != null;
returnButton.setOnClickListener(new View.OnClickListener() { //Nextbutton onclick
public void onClick(View returnButton) {
Intent mainMenu = new Intent(Main3Activity.this, Main2Activity.class);
Main3Activity.this.startActivity(mainMenu);
}
});
}
抛出错误的行是TextView tv = (TextView) questionList.getChildAt(i).findViewById(R.id.checkedTextView1);
有没有人知道为什么会这样?我仍然可以从列表中选择前5-10个项目并将其删除,但是一旦我在ListView中选择了一个底部项目,应用程序就会因此错误而崩溃。
答案 0 :(得分:1)
如评论中所述,您应该从questionList
上的适配器集中获取文本,而不是使用getChildAt(i)
。
尝试做这样的事情
for (int i = questionList.getCount() - 1; i >= 0; i--) {
Cursor c = (Cursor) adapter.getItem(i);
if (checked.get(i)) {
// Get data from cursor
String text = c.getString(c.getColumnIndex(DatabaseHandler.COL_QUESTION));
db.removeData(text);
}
}