在listview上获取空指针

时间:2014-05-23 19:12:42

标签: android android-listview nullpointerexception

我在NullPointerException上收到了listView。我已经附加了我的自定义视图类代码和logcat。请看看,告诉我有什么问题。第57行符合logcat而我的代码行号为57则出错 holder.txtCustomerName.setText(rowItem.getName()); 这是我的代码:

public class All_Fragment_Customview extends ArrayAdapter<All_Fragment_info>{
Context context;
List<All_Fragment_info> objects;    
int layoutResourceId;

    public All_Fragment_Customview(Context context, int layoutResourceId,
            List<All_Fragment_info> rowItems) {
        super(context, layoutResourceId, rowItems);
        this.context = context;
        this.layoutResourceId = layoutResourceId;
        objects = rowItems;
        notifyDataSetChanged();        
    }

public void notifyDataSetChanged()
{
    super.notifyDataSetChanged();
}


   private class ViewHolder {

        TextView txtCustomerName;
        TextView txtmobileNumber;
        TextView txtAddress;
    }
   public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        All_Fragment_info rowItem = objects.get(position);

        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        if (convertView == null) {

            convertView = inflater.inflate(R.layout.customview_all, null);
            holder = new ViewHolder();            
            holder.txtCustomerName = (TextView) convertView.findViewById(R.id.txtName);
            holder.txtmobileNumber = (TextView) convertView.findViewById(R.id.txtNumber);     
            holder.txtAddress = (TextView) convertView.findViewById(R.id.txtAddress);
            convertView.setTag(holder);
        } else
            holder = (ViewHolder) convertView.getTag();
            holder.txtCustomerName.setText(rowItem.getName());
            holder.txtmobileNumber.setText(rowItem.getNumber());
            holder.txtAddress.setText(rowItem.getAddress());
        return convertView;
    }
}

这是我的logcat:

 05-24 00:34:31.820: E/AndroidRuntime(13155): FATAL EXCEPTION: main
 05-24 00:34:31.820: E/AndroidRuntime(13155): java.lang.NullPointerException
 05-24 00:34:31.820: E/AndroidRuntime(13155):   at com.example.test.All_Fragment_Customview.getView(All_Fragment_Customview.java:57)
 05-24 00:34:31.820: E/AndroidRuntime(13155):   at android.widget.AbsListView.obtainView(AbsListView.java:2245)
 05-24 00:34:31.820: E/AndroidRuntime(13155):   at android.widget.ListView.makeAndAddView(ListView.java:1866)
 05-24 00:34:31.820: E/AndroidRuntime(13155):   at android.widget.ListView.fillDown(ListView.java:699)
 05-24 00:34:31.820: E/AndroidRuntime(13155):   at android.widget.ListView.fillFromTop(ListView.java:760)
 05-24 00:34:31.820: E/AndroidRuntime(13155):   at android.widget.ListView.layoutChildren(ListView.java:1682)
 05-24 00:34:31.820: E/AndroidRuntime(13155):   at android.widget.AbsListView.onLayout(AbsListView.java:2075)
 05-24 00:34:31.820: E/AndroidRuntime(13155):   at android.view.View.layout(View.java:14140)
 05-24 00:34:31.820: E/AndroidRuntime(13155):   at android.view.ViewGroup.layout(ViewGroup.java:4467)
 05-24 00:34:31.820: E/AndroidRuntime(13155):   at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1686)
 05-24 00:34:31.820: E/AndroidRuntime(13155):   at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1544)
 05-24 00:34:31.820: E/AndroidRuntime(13155):   at android.widget.LinearLayout.onLayout(LinearLayout.java:1457)
 05-24 00:34:31.820: E/AndroidRuntime(13155):   at android.view.View.layout(View.java:14140)
 05-24 00:34:31.820: E/AndroidRuntime(13155):   at android.view.ViewGroup.layout(ViewGroup.java:4467)
 05-24 00:34:31.820: E/AndroidRuntime(13155):   at android.widget.FrameLayout.onLayout(FrameLayout.java:464)
 05-24 00:34:31.820: E/AndroidRuntime(13155):   at android.view.View.layout(View.java:14140)
 05-24 00:34:31.820: E/AndroidRuntime(13155):   at android.view.ViewGroup.layout(ViewGroup.java:4467)
 05-24 00:34:31.820: E/AndroidRuntime(13155):   at android.support.v4.view.ViewPager.onLayout(ViewPager.java:1141)
 05-24 00:34:31.820: E/AndroidRuntime(13155):   at android.view.View.layout(View.java:14140)
 05-24 00:34:31.820: E/AndroidRuntime(13155):   at android.view.ViewGroup.layout(ViewGroup.java:4467)
 05-24 00:34:31.820: E/AndroidRuntime(13155):   at android.widget.FrameLayout.onLayout(FrameLayout.java:464)
 05-24 00:34:31.820: E/AndroidRuntime(13155):   at android.view.View.layout(View.java:14140)
 05-24 00:34:31.820: E/AndroidRuntime(13155):   at android.view.ViewGroup.layout(ViewGroup.java:4467)
 05-24 00:34:31.820: E/AndroidRuntime(13155):   at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1686)

1 个答案:

答案 0 :(得分:2)

这段代码很好。错误出现在我的xml文件中。实际上同名的“txtName”文本字段已经在另一个xml中可用了,当我检查它时,我发现这是一个问题。