2016-01-12 40 views
0

我找不出为什么它不起作用。ArrayAdapter NullPointerException尝试在空对象引用上调用虚拟方法

java.lang.IllegalStateException:无法执行致活性 的方法:显示java.lang.NullPointerException:尝试调用虚拟方法“无效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; 
} 
} 

回答

0
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;代替。

+0

非常感谢你 –

相关问题