我正在尝试使用创建它的意图填充传递给活动的数据的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;
}
}
}
答案 0 :(得分:3)
您需要在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; }
答案 1 :(得分:2)
将适配器的构造函数更改为此(如果它不是您的活动的内部类):
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);
答案 2 :(得分:0)
首先检查变量是否为空:
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);
}
答案 3 :(得分:0)
要像这样创建适配器
CommentAdapter ca = new CommentAdapter(cmts);
和CommentAdapter
类一起创建像这样的构造函数
public CommentAdapter(ArrayList<Comment> cmts){
this.cmts = cmts;
}
在CommentAdapter类中创建局部变量
private ArrayList<Comment> cmts;