这就是我想要的。我想拥有一个自定义布局视图库。但是我希望能够在库的每个布局元素中的textViews上设置setText。我在图库视图中成功显示空白布局。
但是,当我尝试从布局中的资源上设置文本时,我在setText函数上得到一个NullPointer异常。它说找不到我的textView ..这是我的自定义适配器中getView的代码。
public View getView(int position, View convertView, ViewGroup parent) {
convertView = inflator.inflate(R.layout.adapter_layout, null);
convertView.setLayoutParams(new Gallery.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
TextView text1 = (TextView) findViewById(R.id.text1);
//NULL POINTER HERE !!!!
text1.setText("YOYOYOYOYOYO");
}
答案 0 :(得分:1)
您在活动上调用findViewById()
,而不是充气机返回的视图。试试
TextView text1 = (TextView) convertView.findViewById(R.id.text1);