[英]NullPointerException on populating data in BaseAdapter
我正在嘗試使用通過創建活動的意圖傳遞到活動中的數據填充listview
。 您看到的println
語句確認數據已正確傳遞(即打印了預期的內容,這意味着適配器中引用的ArrayList
已正確初始化)。 但是,我一直在線獲取NullPointerException
content.setText(Html.fromHtml(cmts.get(position).content));
適配器中肯定有問題-可能在getItem()中,或者我對cmts.get(position)
調用沒有按照我的想法做,但是在這一點上我無法弄清楚。
public class CommentsView extends Activity {
ArrayList<Comment> cmts;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_comments_view);
cmts = (ArrayList<Comment>) getIntent().getExtras().getSerializable("clist");
for (Comment c : cmts) {
System.out.println("CMTinCV: " + c.content);
}
ListView lv = (ListView)findViewById(R.id.commentsList);
CommentAdapter ca = new CommentAdapter();
lv.setAdapter(ca);
}
class CommentAdapter extends BaseAdapter {
public CommentAdapter(){
}
@Override
public int getCount() {
return cmts.size()-1;
}
@Override
public Object getItem(int position) {
return cmts.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = getLayoutInflater().inflate(R.layout.commentbox, null);
TextView content = (TextView)findViewById(R.id.commentText);
TextView author = (TextView)findViewById(R.id.commentAuthor);
TextView date = (TextView)findViewById(R.id.commentDate);
content.setText(Html.fromHtml(cmts.get(position).content));
author.setText(cmts.get(position).author.name);
date.setText(cmts.get(position).date);
}
return convertView;
}
}
}
您需要按以下方式在getview方法中訪問textview的內容: convertView.findViewById(R.id.commentText);
這樣訪問它。
@Override public View getView(int position, View convertView, ViewGroup parent) { if (convertView == null) { convertView = getLayoutInflater().inflate(R.layout.commentbox, null); TextView content = (TextView)convertView.findViewById(R.id.commentText); TextView author = (TextView)convertView.findViewById(R.id.commentAuthor); TextView date = (TextView)convertView.findViewById(R.id.commentDate); content.setText(Html.fromHtml(cmts.get(position).content)); author.setText(cmts.get(position).author.name); date.setText(cmts.get(position).date); } return convertView; }
將您的適配器的構造函數更改為此(如果它不是您的活動的內部類):
ArrayList<Comment> cmts;
public CommentAdapter(ArrayList<Comment> mComments){
this.cmts = mComments;
}
這些行:
TextView content = (TextView)findViewById(R.id.commentText);
TextView author = (TextView)findViewById(R.id.commentAuthor);
TextView date = (TextView)findViewById(R.id.commentDate);
應該像:
TextView content = (TextView) convertView.findViewById(R.id.commentText);
TextView author = (TextView) convertView.findViewById(R.id.commentAuthor);
TextView date = (TextView) convertView.findViewById(R.id.commentDate);
首先檢查變量是否為null:
if(cmts.get(position) != null) {
content.setText(Html.fromHtml(cmts.get(position).content));
author.setText(cmts.get(position).author.name);
date.setText(cmts.get(position).date);
}
要這樣創建適配器
CommentAdapter ca = new CommentAdapter(cmts);
和CommentAdapter
類來創建這樣的構造函數
public CommentAdapter(ArrayList<Comment> cmts){
this.cmts = cmts;
}
並在CommentAdapter類中創建局部變量
private ArrayList<Comment> cmts;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.