I have this error:
java.lang.NullPointerException: Attempt to invoke virtual method 'boolean
java.lang.String.equals(java.lang.Object)' on a null object reference
Binary XML file line #0: Attempt to invoke virtual method 'boolean
java.lang.String.equals(java.lang.Object)' on a null object reference
I saw many examples of codes but there is no difference.
the error happened in this line :
row = inflater.inflate(R.layout.list_item, parent, false);
any suggestion, please?
@Override
public View getView(int position, View row, ViewGroup parent){
DataHandler handler = new DataHandler();
try {
if (row == null) {
LayoutInflater inflater = (LayoutInflater) this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = inflater.inflate(R.layout.list_item, parent, false);
handler.poster = (ImageView) row.findViewById(R.id.movieImage);
handler.title = (TextView) row.findViewById(R.id.movieTitle);
handler.rating = (TextView) row.findViewById(R.id.movieRating);
row.setTag(handler);
} else {
handler = (DataHandler) row.getTag();
}
MovieDataProvider dataProvider;
dataProvider = (MovieDataProvider) this.getItem(position);
handler.poster.setImageResource(dataProvider.getMovie_poster());
handler.title.setText(dataProvider.getMovie_title());
handler.rating.setText(dataProvider.getMovie_rating());
}
catch (Exception ex){
Toast.makeText(getContext(),ex.getMessage(),Toast.LENGTH_LONG).show();
}
return row;
}
XML (list_item)
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="80dp"
android:background="#000">
<ImageView
android:id="@+id/movieImage"
android:layout_width="80dp"
android:layout_height="75dp"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:contentDescription=""
android:src="@drawable/movie_0" />
<TextView
android:id="@+id/movieTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginLeft="10dp"
android:layout_toEndOf="@+id/movieImage"
android:text="movie name"
android:textColor="#ff2e5f"
android:textSize="16sp"
android:textStyle="bold" />
<TextView
android:id="@+id/movieRating"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/movieTitle"
android:layout_marginLeft="10dp"
android:layout_toEndOf="@+id/movieImage"
android:text="Rating"
android:textColor="@android:color/holo_blue_dark"
android:textSize="12sp"
android:typeface="sans" />
<view
android:layout_width="match_parent"
android:layout_height="2dp"
android:background="@android:color/background_light"
android:layout_below="@+id/movieImage">
</view>
</RelativeLayout>
The whole code of adapter
import android.content.Context;
import android.support.annotation.NonNull;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.List;
public class MovieAdapter extends ArrayAdapter {
public MovieAdapter(@NonNull Context context, int resource) {
super(context, resource);
}
List list = new ArrayList();
static class DataHandler{
ImageView poster;
TextView title;
TextView rating;
}
public void add(Object object){
super.add(object);
list.add(object);
}
public int getCount(){
return this.list.size();
}
public Object getItem(int position){
return this.list.get(position);
}
@Override
public View getView(int position, View row, ViewGroup parent){
DataHandler handler = new DataHandler();
if (row == null) {
LayoutInflater inflater = (LayoutInflater) this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = inflater.inflate(R.layout.list_item, parent, false);
handler.poster = (ImageView) row.findViewById(R.id.movieImage);
handler.title = (TextView) row.findViewById(R.id.movieTitle);
handler.rating = (TextView) row.findViewById(R.id.movieRating);
row.setTag(handler);
} else {
handler = (DataHandler) row.getTag();
}
MovieDataProvider dataProvider;
dataProvider = (MovieDataProvider) this.getItem(position);
handler.poster.setImageResource(dataProvider.getMovie_poster());
handler.title.setText(dataProvider.getMovie_title());
handler.rating.setText(dataProvider.getMovie_rating());
return row;
}
}