0

I am calling an Arraylist in a CustomAdapter, with custom_row.xml which contains the desired row. I want first two elements of the arraylist side by side in a row, and then third and fourth element in the other row and so on. I wrote this code, just to print the first element from the Arraylist. if I remove the comments and then run, I get the same error as when putting comments. I don't know where I am going wrong.

or maybe what should be the right way.

class CustomAdapter extends ArrayAdapter {

List<String> names;
LayoutInflater inflater;
Context context;
public CustomAdapter(Context context,  List<String> names) {
    super(context,R.layout.custom_row ,names);
   this.names=names;
    this.context=context;
}


@Override
public View getView(int position, View convertView, ViewGroup parent) {


   inflater=LayoutInflater.from(getContext());
    View customview=inflater.inflate(R.layout.custom_row,parent,false);
    String data=names.get(position);
   //String data1=names.get(position+1);
    TextView tv=(TextView)customview.findViewById(R.id.TeamA);
    tv.setText(data);
   //TextView tv1=(TextView)customview.findViewById(R.id.TeamB);
    //tv1.setText(data1);
    return customview;
}

I am getting this error- java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference at com.example.CustomAdapter.getView

1
  • have you initialized name list ? Commented Jun 1, 2016 at 19:34

1 Answer 1

1

The problem is because tv is null, that is because findViewById(R.id.TeamA) returns null, so.. check that TeamA exists in R.layout.custom_row

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.