ArrayAdapter NullPointerException尝试在空对象引用上调用虚方法

时间:2016-01-12 20:56:44

标签: java android nullpointerexception android-arrayadapter

我无法弄清楚它为什么不起作用。

  

java.lang.IllegalStateException:无法执行活动的方法   引起:java.lang.NullPointerException:尝试调用虚方法' void android.widget.ArrayAdapter.add(java.lang.Object)'在com.example.buddy.test.TestDatabaseActivity.addButton(TestDatabaseActivity.java:31)上的空对象引用上

当“添加”按钮单击应用程序崩溃但注释位于数据库中时。

TestDatabaseActivity.java

public class TestDatabaseActivity extends ListActivity {
private CommentsDataSource datasource;
private ArrayAdapter<Comment> adapter = (ArrayAdapter<Comment>) getListAdapter();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_test_database);

    datasource = new CommentsDataSource(this);
    datasource.open();

    List<Comment> values = datasource.getAllComments();

    ArrayAdapter<Comment> adapter = new ArrayAdapter<Comment>(this, android.R.layout.simple_list_item_1, values);
    setListAdapter(adapter);
}

public void addButton(View view){
    Comment comment;
    comment = datasource.createComment("My comment");
    adapter.add(comment);
    adapter.notifyDataSetChanged();
}

public void deleteButton(View view){
    Comment comment;
    if (getListAdapter().getCount() > 0) {
        comment = (Comment) getListAdapter().getItem(0);
        datasource.deleteComment(comment);
        adapter.remove(comment);
    }
    adapter.notifyDataSetChanged();
}

@Override
protected void onResume() {
    datasource.open();
    super.onResume();
}

@Override
protected void onPause() {
    datasource.close();
    super.onPause();
}
}

CommentsDataSource.java

public class CommentsDataSource {
private SQLiteDatabase database;
private MySQLiteHelper dbHelper;
private String[] allColumns = { MySQLiteHelper.COLUMN_ID, MySQLiteHelper.COLUMN_COMMENT };

public CommentsDataSource(Context context) {
    dbHelper = new MySQLiteHelper(context);
}

public void open() throws SQLException {
    database = dbHelper.getWritableDatabase();
}

public void close() {
    dbHelper.close();
}

public Comment createComment(String comment) {
    ContentValues values = new ContentValues();
    values.put(MySQLiteHelper.COLUMN_COMMENT, comment);
    long insertId = database.insert(MySQLiteHelper.TABLE_COMMENTS, null, values);
    Cursor cursor = database.query(MySQLiteHelper.TABLE_COMMENTS, allColumns, MySQLiteHelper.COLUMN_ID + " = " + insertId, null, null, null, null);
    cursor.moveToFirst();
    Comment newComment = cursorToComment(cursor);
    cursor.close();
    return newComment;
}

public void deleteComment(Comment comment) {
    long id = comment.getId();
    System.out.println("Comment deleted with id: " + id);
    database.delete(MySQLiteHelper.TABLE_COMMENTS, MySQLiteHelper.COLUMN_ID + " = " + id, null);
}

public List<Comment> getAllComments() {
    List<Comment> comments = new ArrayList<Comment>();

    Cursor cursor = database.query(MySQLiteHelper.TABLE_COMMENTS, allColumns, null, null, null, null, null);

    cursor.moveToFirst();
    while (!cursor.isAfterLast()) {
        Comment comment = cursorToComment(cursor);
        comments.add(comment);
        cursor.moveToNext();
    }
    cursor.close();
    return comments;
}

private Comment cursorToComment(Cursor cursor) {
    Comment comment = new Comment();
    comment.setId(cursor.getLong(0));
    comment.setComment(cursor.getString(1));
    return comment;
}
}

Comment.java

public class Comment {
private long id;
private String comment;

public long getId() {
    return id;
}

public void setId(long id) {
    this.id = id;
}

public String getComment() {
    return comment;
}

public void setComment(String comment) {
    this.comment = comment;
}

@Override
public String toString() {
    return comment;
}
}

1 个答案:

答案 0 :(得分:1)

ArrayAdapter<Comment> adapter = new ArrayAdapter<Comment>(this, android.R.layout.simple_list_item_1, values);

在这里,您要将新的ArrayAdapter分配给本地变量,而不是您的字段。摆脱ArrayAdapter<Comment>,将值分配给字段。

private ArrayAdapter<Comment> adapter = (ArrayAdapter<Comment>) getListAdapter();

在这里,您正在尝试初始化该字段。但是,getListAdapter()保证在此处返回null,因为您尚未调用setListAdapter()。仅使用private ArrayAdapter<Comment> adapter;替换它。