0

I tried to build custom ListView in Fragment but I have some Error

when my app run it crash and give me this line error:

java.lang.NullPointerException:

and it's take me to this line in adapter code:

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

What am I doing wrong?

This is my Fragment:

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;
    }
}

This is my Adapter

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;
    }
}

This is my cources

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

This is my XML of list_adapter_view

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="@android:color/white">

<TextView
    android:text="TextView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/Tx_name_planche"
    tools:ignore="HardcodedText"
    android:textSize="24sp"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true" />

<TextView
    android:text="TextView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/Tx_price_baget"
    tools:ignore="HardcodedText"
    android:textSize="18sp"
    android:layout_marginRight="38dp"
    android:layout_marginEnd="50dp"
    android:layout_alignParentTop="true"
    android:layout_toLeftOf="@+id/Tx_price_plate"
    android:layout_toStartOf="@+id/Tx_price_plate" />

<TextView
    android:text="TextView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/Tx_price_plate"
    tools:ignore="HardcodedText"
    android:textSize="18sp"
    android:layout_marginRight="31dp"
    android:layout_marginEnd="31dp"
    android:layout_alignParentTop="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true" />

</RelativeLayout>
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
sharon2469
  • 89
  • 1
  • 11

1 Answers1

2

In the constructor ListviewContactAdapter, you need to initialize the mInflate variable:

mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE)
Lesther Vega
  • 528
  • 3
  • 14
  • Now your line make me error: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Object android.content.Context.getSystemService(java.lang.String)' on a null object reference – sharon2469 Jun 23 '16 at 20:34
  • Replace your adapter creation with: `ListviewContactAdapter ad = new ListviewContactAdapter(getActivity(),listContact);` – Juan Cruz Soler Jun 23 '16 at 21:15
  • Omg you myh hero hhhhhh Thanks you very helpful Can you explain me what i change in this line? – sharon2469 Jun 23 '16 at 22:00
  • @sharon2469 The same problem, when you use a variable, the initialization is necessary, if you only declare "private Context context;" by default is null because is a object, you need to set a value before to use: "context = getActivity()" by example. – Lesther Vega Jun 24 '16 at 15:27
  • @LestherVega Ok now i am understand Thanks – sharon2469 Jun 24 '16 at 15:41