when i create the custom view of each items of the list view, i get a null pointer exception and i dont know why, the layout id seems correct
import android.
In your code
itemView = getLayoutInflater().inflate(R.layout.item_view, parent, false);
the getLayoutInflater() method is not usable in The inner adapter class. Hence you should try creating an Variable to have the Layout inflater object of the parent class and hence access it ,or you can send in the context of the calling class in the constructor of the adapter class.
Hence you can use either like this:
public class ActivityMainWish extends Activity {
LayoutInflate inflater;
private List<Wish> myWishs = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
inflater=getLayoutInflater()
...
}
and use this inflater
variable over here:
itemView = inflater.inflate(R.layout.item_view, parent, false);
Or like this:
private class MyListAdapter extends ArrayAdapter<Wish>{
private Context mContext;
public MyListAdapter(Context ctx){
super(ActivityMainWish.this, R.layout.item_view, myWishs);
mContext=ctx;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View itemView = convertView;
if (itemView==null)
itemView = LayoutInflater.from(mContext).inflate(R.layout.item_view, parent, false);
and send the Context object like:
ArrayAdapter<Wish> adapter = new MyListAdapter(getApplicationContext());
Try changing the relevant code inside your Adapter class to the following
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View itemView;
if (convertView == null) {
itemView = inflater.inflate(R.layout.item_view, parent, false);
} else {
itemView = convertView;
}
...
...
return itemView;
}