尝试setText时出现NullPointerException

时间:2014-11-22 23:54:33

标签: android textview android-arrayadapter getview

textView应该被实例化,我不知道为什么它在textView.setText中显示NullPointerException(" 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 个答案:

答案 0 :(得分:0)

尝试:

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

使用findViewById获取观看次数。