android TextView.setText Nullpointer异常(android初学者)

时间:2016-03-04 16:50:10

标签: android nullpointerexception textview

 @Override
public View getView(final int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub

    if(convertView == null){
        LayoutInflater inflater=(LayoutInflater)c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView=inflater.inflate(R.layout.program_list,null);
    }
    // GET VIEWS
    TextView nameTxt=(TextView) convertView.findViewById(R.id.nametxt);
    TextView landTxt=(TextView) convertView.findViewById(R.id.landtxt);
    TextView groesseTxt=(TextView) convertView.findViewById(R.id.groessetxt);
    TextView gewichtTxt=(TextView) convertView.findViewById(R.id.gewichttxt);
    ImageView img =(ImageView) convertView.findViewById(R.id.imageView1);

    //SET DATA
    nameTxt.setText(names[position]);
    landTxt.setText(land[position]);
    groesseTxt.setText(groesse[position]);
    gewichtTxt.setText(gewicht[position]);
    img.setImageResource(images[position]);

    return convertView;
}

I can't start my program of the NullPointerException

enter image description here

我想创建一个自定义列表视图,使用onClick到新的活动...

3 个答案:

答案 0 :(得分:0)

检查您的布局(R.layout.program_list)是否包含id nametxt,landtxt,groessetxt和gewichttxt可能这是错误,第98行中缺少某人。

如果全部存在,则另一个是某些数组包含空值。

答案 1 :(得分:0)

调试此代码:

TextView nameTxt=(TextView) convertView.findViewById(R.id.nametxt);
TextView landTxt=(TextView) convertView.findViewById(R.id.landtxt);
TextView groesseTxt=(TextView) convertView.findViewById(R.id.groessetxt);
TextView gewichtTxt=(TextView) convertView.findViewById(R.id.gewichttxt);
ImageView img =(ImageView) convertView.findViewById(R.id.imageView1);

也许他们的观点(nametxt,landtxt,groessetxt,gewichttxt,imageView1)在program_list布局中不存在。

答案 2 :(得分:0)

您的一个或多个文字视图不属于您的布局。确保在布局文件中提供了相同的ID。