Reputation: 32680
I'm trying to populate a listview
with data passed into the activity via a the intent that created it. The println
statement you see confirms that the data is passed in correctly (i.e. the expected content is printed, meaning that the ArrayList
referenced in the adapter is properly initialized). However, I keep getting a NullPointerException
on the line
content.setText(Html.fromHtml(cmts.get(position).content));
There must be something wrong in the adapter - maybe in the getItem(), or perhaps my calls to cmts.get(position)
isn't doing what I think it is, but at this point I can't figure it out.
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;
}
}
}
Upvotes: 1
Views: 370
Reputation: 17037
Change your Adapter's constructor to this (if it's not an inner class for your activity) :
ArrayList<Comment> cmts;
public CommentAdapter(ArrayList<Comment> mComments){
this.cmts = mComments;
}
and these lines :
TextView content = (TextView)findViewById(R.id.commentText);
TextView author = (TextView)findViewById(R.id.commentAuthor);
TextView date = (TextView)findViewById(R.id.commentDate);
should be like :
TextView content = (TextView) convertView.findViewById(R.id.commentText);
TextView author = (TextView) convertView.findViewById(R.id.commentAuthor);
TextView date = (TextView) convertView.findViewById(R.id.commentDate);
Upvotes: 2
Reputation: 11
To create the adapter like this
CommentAdapter ca = new CommentAdapter(cmts);
And CommentAdapter
class to create constructor like this
public CommentAdapter(ArrayList<Comment> cmts){
this.cmts = cmts;
}
And create local variable in CommentAdapter class
private ArrayList<Comment> cmts;
Upvotes: 0
Reputation: 23648
You need to access the textview's in your getview method as below:
convertView.findViewById(R.id.commentText);
access it like this.
@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; }
Upvotes: 3
Reputation: 15973
Check if the variable is null or not first:
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);
}
Upvotes: 0