我有一个自定义适配器类,在里面getView
我遇到了查看器和设置/访问元素的问题。NullPointerException
当我尝试将 设置为查看器时,我始终收到一个textView
。我已经测试了数据并逐步记录了该过程,似乎视图设置不正确,或者它试图访问的元素不是?logcat 中的前几行显示:
04-13 20:55:13.620: W/dalvikvm(23329): threadid=1: thread exiting with uncaught exception (group=0x40dd8930)
04-13 20:55:13.644: E/AndroidRuntime(23329): FATAL EXCEPTION: main
04-13 20:55:13.644: E/AndroidRuntime(23329): java.lang.NullPointerException
04-13 20:55:13.644: E/AndroidRuntime(23329): at com.idealiner.metrosleepuniversal.adapter.LocationModelGridAdapter.getView(LocationModelGridAdapter.java:99)
第 99 行是我尝试访问 textView 元素时:
viewHolder.textView.setText(locationModel.col_line);
我已经测试过locationModel.col_line
它不会返回空值。似乎Viewholder
无法访问...或者找不到 textView ?我不完全确定。
整个 getView 方法:
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
ViewHolder viewHolder;
if (view == null || view.getTag() == null) {
view = mInflater.inflate(R.layout.grid_view, parent, false);
viewHolder = new ViewHolder();
// viewHolder.imageView = (ImageView) view.findViewById(R.id.grid_image);
viewHolder.textView = (TextView) view.findViewById(R.id.grid_label);
view.setTag(viewHolder);
Log.v("GetViewAdapter", "View is null, setup viewholder");
}else {
viewHolder = (ViewHolder) view.getTag();
Log.v("GetViewAdapter", "View is not null, setup viewholder");
}
lineModel locationModel = mLine[position];
Log.v("GetViewAdapter", "mline has a position of: " + position);
Log.v("GetViewAdapter", "mline has a name of: " + locationModel.col_line);
if(locationModel!=null) {
viewHolder.textView.setText(locationModel.col_line);
}
return view;
}
该类的其余代码可以在这里找到:https ://gist.github.com/markbratanov/5380875
我整天都在思考这个问题,所以任何建议/提示将不胜感激!谢谢。