Context must not be null

3

You see, I have a class which works properly for me, but, the images that are not seen on the screen are happening as if I were looking for the correct one, so I added the library of Picasso :

Picasso.with(context)
    .load(Estadisticas_List.get(position).getEscudo_local())
    .placeholder(R.drawable.ic_contact_icon)
    .into(holder.escudo_local);

The application closes, and this is the logcat :

java.lang.IllegalArgumentException: Context must not be null

And this the complete code:

public class Estadisticas_Adapter extends ArrayAdapter<Estadisticas> {

    Context context;

    ArrayList<Estadisticas> Estadisticas_List;
    LayoutInflater vi;
    int Resource;
    ViewHolder holder;

    public Estadisticas_Adapter(Context context, int resource, ArrayList<Estadisticas> objects) {
        super(context, resource, objects);
        vi = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        Resource = resource;
        Estadisticas_List = objects;
    }


    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // convert view = design
        View v = convertView;
        if (v == null) {
            holder = new ViewHolder();
            v = vi.inflate(Resource, null);

            holder.escudo_local = (ImageView) v.findViewById(R.id.tv_esc_local);
            holder.escudo_visi = (ImageView) v.findViewById(R.id.tv_esc_visi);

            holder.tv_Nombre = (TextView) v.findViewById(R.id.tv_Equipo_Local);
            holder.tv_Nombre_Local = (TextView) v.findViewById(R.id.tv_Equipo_Local);
            holder.tv_Nombre_Visi = (TextView) v.findViewById(R.id.tv_Equipo_Visi);
            //holder.tvNombre_Arbitro = (TextView) v.findViewById(R.id.tv_Nombre_Arbitro);
            holder.tv_Fecha = (TextView) v.findViewById(R.id.tv_Fecha);
            holder.tv_Hora = (TextView) v.findViewById(R.id.tv_Hora);
            holder.tv_Result_Local = (TextView) v.findViewById(R.id.tv_Result_Local);
            holder.tv_Result_Visitante = (TextView) v.findViewById(R.id.tv_Result_Visitante);
            /*holder.tv_Targeta = (TextView) v.findViewById(R.id.tv_targeta);
            holder.tv_Tar_Minuto = (TextView) v.findViewById(R.id.tv_tar_minuto);
            holder.tv_Gol_Minuto = (TextView) v.findViewById(R.id.tv_gol_minuto);
            holder.tv_Nombre_Entra = (TextView) v.findViewById(R.id.tv_nombre_entra);
            holder.tv_Dorsal_Entra = (TextView) v.findViewById(R.id.tv_dorsal_entra);
            holder.tv_Nombre_Sale = (TextView) v.findViewById(R.id.tv_nombre_sale);
            holder.tv_Dorsal_Sale = (TextView) v.findViewById(R.id.tv_dorsal_sale);
            holder.tv_Cambio_Min = (TextView) v.findViewById(R.id.tv_cambio_min);*/
            holder.tv_Estado_Partido = (TextView) v.findViewById(R.id.tv_Estado_Partido);
            //holder.tv_Num_Goles = (TextView) v.findViewById(R.id.tv_Num_Goles);
            v.setTag(holder);
        } else {
            holder = (ViewHolder) v.getTag();
        }

        Picasso.with(context)
                .load(Estadisticas_List.get(position).getEscudo_local())
                .placeholder(R.drawable.ic_contact_icon)
                .into(holder.escudo_local);

        //holder.escudo_local.setImageResource(R.mipmap.ic_launcher);
        holder.escudo_visi.setImageResource(R.mipmap.ic_launcher);

        //new DownloadImageTask(holder.escudo_local).execute(Estadisticas_List.get(position).getEscudo_local());
        new DownloadImageTask(holder.escudo_visi).execute(Estadisticas_List.get(position).getEscudo_visitante());

        //holder.tvNombre_Arbitro.setText(Estadisticas_List.get(position).getNombre_Arbitro());
        holder.tv_Nombre.setText(Estadisticas_List.get(position).getNombre());
        holder.tv_Nombre_Local.setText(Estadisticas_List.get(position).getNombre_Local());
        holder.tv_Nombre_Visi.setText(Estadisticas_List.get(position).getNombre_Visi());
        holder.tv_Fecha.setText("Fecha: "+Estadisticas_List.get(position).getFecha());
        holder.tv_Hora.setText("Hora: "+Estadisticas_List.get(position).getHora());
        holder.tv_Result_Local.setText(Estadisticas_List.get(position).getResult_Local());
        holder.tv_Result_Visitante.setText(Estadisticas_List.get(position).getResult_Visitante());
        //holder.tv_Targeta.setText("targeta: " + Estadisticas_List.get(position).getTargeta());
        //holder.tv_Tar_Minuto.setText("Targetamin: " + Estadisticas_List.get(position).getTarMinuto());
        //holder.tv_Gol_Minuto.setText("golmin: " + Estadisticas_List.get(position).getGol_Min());
        //holder.tv_Nombre_Entra.setText("Nombreentra: " + Estadisticas_List.get(position).getNombre_Entra());
        //holder.tv_Dorsal_Entra.setText("dorsalentra: " + Estadisticas_List.get(position).getDorsal_Entra());
        //holder.tv_Nombre_Sale.setText("Nombreentrasale: " + Estadisticas_List.get(position).getNombre_Sale());
        //holder.tv_Dorsal_Sale.setText("dorsalsale: " + Estadisticas_List.get(position).getDorsal_Sale());
        //holder.tv_Cambio_Min.setText("cambiomin: " + Estadisticas_List.get(position).getCambio_Min());
        holder.tv_Estado_Partido.setText(Estadisticas_List.get(position).getEstado_Partido());
        //holder.tv_Num_Goles.setText(Estadisticas_List.get(position).getNum_Goles());
        return v;

    }

    static class ViewHolder {
        public TextView tv_Nombre;
        public TextView tv_Nombre_Local;
        public TextView tv_Result_Local;
        public TextView tv_Result_Visitante;
        public TextView tvNombre_Arbitro;
        public TextView tv_Fecha;
        public TextView tv_Hora;
        public TextView tv_Nombre_Visi;
        public TextView tv_Targeta;
        public TextView tv_Tar_Minuto;
        public TextView tv_Gol_Minuto;
        public TextView tv_Nombre_Entra;
        public TextView tv_Dorsal_Entra;
        public TextView tv_Nombre_Sale;
        public TextView tv_Dorsal_Sale;
        public TextView tv_Cambio_Min;
        public TextView tv_Estado_Partido;
        public TextView tv_Num_Goles;

        public ImageView escudo_local;
        public ImageView escudo_visi;

    }

    private class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {
        ImageView bmImage;


        public DownloadImageTask(ImageView bmImage) {
            this.bmImage = bmImage;

        }

        protected Bitmap doInBackground(String... urls) {
            String urldisplay = urls[0];
            Bitmap mIcon11 = null;
            try {
                InputStream in = new java.net.URL(urldisplay).openStream();
                mIcon11 = BitmapFactory.decodeStream(in);
            } catch (Exception e) {
                Log.e("Error", e.getMessage());
                e.printStackTrace();
            }
            return mIcon11;
        }

        protected void onPostExecute(Bitmap result) {
            bmImage.setImageBitmap(result);

        }

    }
}

Can you give me a solution?

Thanks Angel Angel, the solution:

public Estadisticas_Adapter(Context context, int resource, ArrayList<Estadisticas> objects) {
        super(context, resource, objects);
        vi = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        Resource = resource;
        Estadisticas_List = objects;
        this.context = context;
    }
    
asked by Rafel C.F 17.01.2016 в 08:26
source

1 answer

3

This part may be the one that is throwing you the error:

//..
Context context;
//..
public Estadisticas_Adapter(Context context, int resource,  ArrayList<Estadisticas> objects) {

    super(context, resource, objects);

You hide the context that initializes in super, you could try to use this.context = context under the constructor, if this works for you, you could refactor the code if necessary to not call context in the base class or maybe you can do it protect and not use Contex context in class Extadisticas_Adapter because it inherits it (I do not know how it is in the base class, I just leave it as an observation), or eliminate the Contex contex of the class and use the base class but I do not know if this is possible because I do not know which way is in the base class or what you will do with contex in class Extadisticas_Adapter , but basically I think it is because Contex contex is not initialized in this class but in the base class, but even if in the base class it is visible to the derived class, it is being hidden, and the one that is visible Contex contex is not initialized.

    
answered by 17.01.2016 / 13:04
source