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

标签 java android nullpointerexception android-arrayadapter

<分区>

我不明白为什么它不起作用。

java.lang.IllegalStateException: Could not execute method of the activity Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ArrayAdapter.add(java.lang.Object)' on a null object reference at 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;
}
}

评论.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;
}
}

最佳答案

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; .

关于java - ArrayAdapter NullPointerException 尝试在空对象引用上调用虚拟方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34753637/

相关文章:

java - 通过给定颜色 Int 的因子获得更深的颜色

java - 将序列化 java 对象保存到 Db 时出现问题

安卓删除查询

java - 使上传的图像在布局/Activity 更改后保留

java - 什么是NullPointerException,我该如何解决?

java - 空点异常...不知道为什么

java - 在 Java 8 中向 Map 添加非值的优雅方法?

java - 使用 Spring 动态添加/更改表列

java - Wicket 口 boolean 模型

android - Fragment 的 onResume() 中的 getview()