画廊中的自定义布局问题,Android!
这就是我想要的。我想要一个布局的自定义视图库。不过,我希望能够在图库的每个布局元素内的 textViews 上设置 setText。我正在图库视图中成功显示空白布局。
但是,当我尝试从布局中对资源进行 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");
}
Here is what I want. I want to have a gallery of custom views of layouts. However I want to be able to set setText on textViews within each layout element of the gallery. I am displaying the blank layouts successfully in a gallery view.
However, when I try to setText on a resource from the layout I get a NullPointer exception on the setText function. It says it cannot find my textView.. Here is my code for the getView in my custom Adapter for my gallery.
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");
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您正在对 Activity 调用
findViewById()
,而不是对充气器返回的视图。尝试You're calling
findViewById()
on the activity, not the view returned by the inflator. Try