Fragment中自定义ListView的错误

时间:2016-06-23 20:16:25

标签: android android-fragments android-activity

我尝试在Fragment中构建自定义ListView但我有一些错误

当我的应用程序运行崩溃并给我这行错误时:

  

显示java.lang.NullPointerException:

它将我带到适配器代码中的这一行:

subscribe()

我做错了什么?

这是我的片段:

convertView = mInflater.inflate(R.layout.list_adapter_view, null);

这是我的适配器

public class MyOrderFragment extends Fragment {
    TextView name;
    ListView listOrder;
    private Context context;
    List<cources> collegeList;

    public MyOrderFragment() {
        // Required empty public constructor
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EventBus.getDefault().register(this);


    }
    @Subscribe
    public void onMessageEvent(SendDataHelper event){

    }
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.my_order_fragment, container, false);
        name = (TextView) rootView.findViewById(R.id.textView);

        listOrder = (ListView) rootView.findViewById(R.id.listOrder);

        ArrayList<cources> listContact = GetlistContact();
        ListviewContactAdapter ad = new ListviewContactAdapter(context,listContact);
        listOrder.setAdapter(ad);

        return rootView;
    }

    private ArrayList<cources> GetlistContact(){
        ArrayList<cources> contactlist = new ArrayList<cources>();

        cources contact = new cources();

        contact.name="sharon";
        contact.price_plate="8";
        contactlist.add(contact);

        contact = new cources();
        contact.name = "sharon2";
        contact.price_plate = "10";
        contactlist.add(contact);

        contact = new cources();
        contact.name = "sheli";
        contact.price_plate="85";
        contactlist.add(contact);

        return contactlist;
    }
}

这是我的资源

public class ListviewContactAdapter extends BaseAdapter {

    Context context;
    List<cources> valueList;

    private LayoutInflater mInflater;

    public ListviewContactAdapter(Context context, ArrayList<cources> listValue){
        this.valueList = listValue;
        this.context = context;
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return valueList.size();
    }

    @Override
    public Object getItem(int arg0) {
        // TODO Auto-generated method stub
        return valueList.get(arg0);
    }

    @Override
    public long getItemId(int arg0) {
        // TODO Auto-generated method stub
        return arg0;
    }


    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        ViewHolder holder;
        if(convertView == null){
            convertView = mInflater.inflate(R.layout.list_adapter_view, null);
            holder = new ViewHolder();
            holder.txtname = (TextView) convertView.findViewById(R.id.Tx_name_planche);
            holder.txtplate = (TextView) convertView.findViewById(R.id.Tx_price_plate);
            holder.txtbaget = (TextView) convertView.findViewById(R.id.Tx_price_plate);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        holder.txtname.setText(valueList.get(position).name);
        holder.txtplate.setText(valueList.get(position).price_plate);
        holder.txtbaget.setText(valueList.get(position).price_baget);

        return convertView;
    }

    static class ViewHolder{
        TextView txtname, txtplate,txtbaget;
    }
}

这是我的list_adapter_view XML

public class cources {
    public String name;
    public String price_plate;
    public String price_baget;
}

1 个答案:

答案 0 :(得分:2)

在构造函数ListviewContactAdapter中,您需要初始化mInflate变量:

mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE)