NullPointerException when trying to setText

98 Views Asked by At

The textView shoud be instanced, I don't know why it says NullPointerException at textView.setText("test");

public View getView(int position,  View convertView, ViewGroup parent) {

    LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View rowView = inflater.inflate(R.layout.list_navdrawer, parent, false);

    TextView textView = (TextView) rowView.getTag(android.R.id.text1);
    textView.setText("test");

    if (position == mSelectedItem) {
        textView.setTextColor(getContext().getResources().getColor(android.R.color. holo_blue_dark));
        textView.setTypeface(Typeface.DEFAULT_BOLD);
    } else {
        textView.setTextColor(getContext().getResources().getColor(android.R.color.white));
    }

    return textView;
}
1

There are 1 best solutions below

0
On BEST ANSWER

Try:

TextView textView = (TextView) rowView.findViewById(android.R.id.text1); 

Use findViewById to get views.