tags:

views:

35

answers:

2

Hi experts, I am explaining my issue straightaway. Please refer the following code snippet.

@Override
public View getView(int index, View convertView, ViewGroup parent) {
    Comments comment = comments.get(index);
    if (convertView == null) {
        LayoutInflater inflator = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = inflator.inflate(R.layout.comment_row, null);
    }

    TextView tvAuthor = (TextView) convertView.findViewById(R.id.commentAuthor);
    System.out.println("tvAuthor"+tvAuthor);
    tvAuthor.setText(comment.getAuthor());
    convertView.setTag(comment);
    return convertView;
}

I get null for tvAuthor . As a result, in the immediate next line where I try to setText, I get null pointer exception.

I have declared commentAuthor in the xml correctly. I cannot trace, from where this error pops up. Experts, kindly help.

ny help in this regard is well appreciated.

Look forward, Regards, Rony

A: 

Make sure that View.findViewById() is called after View.onCreate() since this is when XML is parsed.

Preferably you'd put .findViewById() inside .onCreate() and save the result in a field for use elsewhere.

Peter Knego