你的位置:首页 > 操作系统

[操作系统]Android开发报错系列(一),java.lang.NullPointerException,at android.widget.ListView.setupChild

问题描述:运行代码是报空指针错误,java.lang.NullPointerException,at Android.widget.ListView.setupChild

问题定位:listview控件报错,查找原因并没有问题,怀疑adapter设置item值时出错,检查代码,果然是这里报错

在设置完adapter的item值得时候,由于粗心,return convertView 时第一次返回成了  return null,导致了上述错误

 1 public class CasesAdapter extends NormalListAdapter<Cases>{ 2   private ViewHolder viewHolder = null; 3   private Context mCtc; 4   private static class ViewHolder { 5     public ImageView aImg; 6     public TextView aTitle; 7     public TextView aHits; 8     public TextView aTime; 9   }10   public CasesAdapter(Context context, List<Cases> item) {11     super(context, item);12     // TODO Auto-generated constructor stub13     mCtc = context;14   }15 16   @Override17   public View getView(int position, View convertView, ViewGroup parent) {18     //判断试图是否为空19     if (convertView==null) {20       //如果试图为空的情况下,加载试图页面21       convertView=inflater.inflate(R.layout.list_item_cases, null);22       // 实例化 ViewHolder23       viewHolder = new ViewHolder();24       // 设置viewholder中元素对应的控件25       viewHolder.aImg=(ImageView) convertView.findViewById(R.id.imgs);26       viewHolder.aTitle=(TextView) convertView.findViewById(R.id.msg_title);27       viewHolder.aHits=(TextView) convertView.findViewById(R.id.msg_hit);28       viewHolder.aTime=(TextView) convertView.findViewById(R.id.msg_time);29       convertView.setTag(viewHolder);30     }31     else {32       viewHolder=(ViewHolder) convertView.getTag();33     }34     Cases cs=itemContent.get(position);35     //设置控件绑定的数值36     viewHolder.aTitle.setText(cs.getTitle());37     viewHolder.aHits.setText(cs.getHits());38     viewHolder.aTime.setText(cs.getAddtime());39     // 网络异步加载40         ImageLoader.getInstance().displayImage("http://192.168.2.26:8012"+cs.getFilename(),41             viewHolder.aImg, Define.options1,42             new AnimateFirstDisplayListener());43     return null;//该处报错,引起异常44   }45 46 }