Reputation: 3
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
Upvotes: 0
Views: 1474
Reputation: 9150
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
Upvotes: 1