2014-11-22 66 views
-1

textView应该是实例,我不知道为什么它说textView.setText(“测试”)NullPointerException;NullPointerException当尝试setText

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; 
} 
+0

使用调试器。它是空的,因为textview在当前布局中不存在,或者它的ID不是android.R.id.text1。无论哪种方式,调试器会立即显示给您。 – Simon 2014-11-22 23:59:19

+0

可能重复[什么是空指针异常,以及如何解决它?](http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do -i-fix-it) – Simon 2014-11-22 23:59:51

+0

“android.R.id.text1”应该只是“R.id.text1”,不是吗? – Hacketo 2014-11-23 00:24:24

回答

0

尝试:

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

使用findViewById得到的意见。

+0

这个工程。谢谢 – 2014-11-23 01:19:03

相关问题