0

My idea is using a AsynTask download a dataset and store in a arrayList. That I have checked and works well. In the onPostExecute method I call a function to update my listView and I load the data stored in the ArrayList.

Code:

protected void onPostExecute(String result) 
{       
    progressDialog.setProgress(99);
    actualizarDisplay();
}

The method actulizarDisplay:

dataAdapter = new MyCustomAdapter(this,R.layout.servicio, listaServicios);
ListView listView = (ListView) findViewById(R.id.listaServ);
listView.setAdapter(dataAdapter);

And the class MyCustomAdapter:

private class MyCustomAdapter extends ArrayAdapter<Servicio> 
{

private ArrayList<Servicio> servicioList;

public MyCustomAdapter(Context context, int textViewResourceId,ArrayList<Servicio> servicioList) 
{
    super(context, textViewResourceId,servicioList);
    this.servicioList = new ArrayList<Servicio>();
    this.servicioList.addAll(servicioList);
}

private class ViewHolder 
{
   TextView nombre, hora, lugar, fecha;
   ImageView tipo;   
}

@Override
public View getView(int position, View convertView, ViewGroup parent) 
{
    ViewHolder holder = null;

    if (convertView == null) 
    {
       LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
       convertView = vi.inflate(R.layout.servicio, null);

       holder = new ViewHolder();
       holder.nombre = (TextView) convertView.findViewById(R.id.titulo);
       holder.lugar = (TextView) convertView.findViewById(R.id.lugar);
       holder.hora = (TextView) convertView.findViewById(R.id.hora);
       holder.fecha = (TextView) convertView.findViewById(R.id.fecha);
       convertView.setTag(holder);
    }
     else 
     {
        holder = (ViewHolder) convertView.getTag();
     }

     Servicio servicio = servicioList.get(position);
Log.i("ConvertView", servicio.getNombre()); // the correct value is displayed
     holder.nombre.setText(servicio.getNombre());
     holder.hora.setText(servicio.getHora());
     holder.lugar.setText(servicio.getLugar());
     holder.fecha.setText(servicio.getFecha());

     if (servicio.tipo == 0)
           holder.tipo.setImageResource(R.drawable.cultural);

     return convertView;

}   

}

And this is the error in the logcat, THE LINE 423 is: holder.nombre.setText(servicio.getNombre());

FATAL EXCEPTION: main java.lang.NullPointerException at app.serviciosprote.Inicio$MyCustomAdapter.getView(Inicio.java:423)
at android.widget.AbsListView.obtainView(AbsListView.java:2350) at android.widget.ListView.measureHeightOfChildren(ListView.java:1409) at android.widget.ListView.onMeasure(ListView.java:1273) at android.view.View.measure(View.java:15286) at android.widget.RelativeLayout.measureChild(RelativeLayout.java:602) at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:415) at android.view.View.measure(View.java:15286) at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4832) at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1390) at android.widget.LinearLayout.measureVertical(LinearLayout.java:681) at android.widget.LinearLayout.onMeasure(LinearLayout.java:574) at android.view.View.measure(View.java:15286) at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4832) at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
at android.view.View.measure(View.java:15286) at android.widget.LinearLayout.measureVertical(LinearLayout.java:833) at android.widget.LinearLayout.onMeasure(LinearLayout.java:574) at android.view.View.measure(View.java:15286) at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4832) at android.widget.FrameLayout.onMeasure(FrameLayout.java:310) at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2269)

Thanks for trying to help

Alex Lockwood
  • 83,063
  • 39
  • 206
  • 250
mdl
  • 45
  • 8
  • I think the problem is already solved and was that if the text of the TextView is very long and does not fit fails – mdl Dec 16 '14 at 21:24
  • your explanation does not really make sense. what you describe cannot cause the stacktrace you posted. – njzk2 Dec 16 '14 at 21:27
  • with regards to event on click listener, google it, you will find enough tuts to help you..asking another question might be a dupe.. – Elltz Dec 16 '14 at 22:22

1 Answers1

0
Comment  

This probably means your first textview is not in this xml file

servicio.xml

does not have a textview with an id of titulo

 holder.nombre = (TextView) convertView.findViewById(R.id.titulo);

hence the nullpointer

double check that you are calling the correct textview with the correct id in the correct xml

Sbonelo
  • 664
  • 1
  • 9
  • 25