Problems with SetTag () in Android Studio

1

I am filling out data in a listview and I get an error when I'm making the following code with the error:

  

FATAL EXCEPTION: main Process: com.tuinen.carlosvilla.tuinensoft, PID:   21682 java.lang.NullPointerException: Attempt to read from field   'android.widget.TextView   com.tuinen.carlosvilla.tuinensoft.BandasAdapter $ BandasHolder.text 'on   a null object reference

    
asked by CarlosVilla 19.12.2016 в 17:42
source

2 answers

1

When convertView is null , it means that there is no view in recycling, therefore we must create a new one. Your Holder has 2 visual attributes ImageView and% TextView . The function of the adapter itself is to reuse views. Therefore, once an item of your list is loaded, it is not necessary to define this view again, but it is already recycled and it is enough to set the values of your different items. Your code evaluates that it is not null , if it is null you define this format of view so that the other items load it, if it is different from null , only you take the one that you created previously with getTag() .

The error is in the else where you do

holder = (BandasHolder) row.setTag();

Change to:

holder = (BandasHolder) row.getTag();
    
answered by 19.12.2016 в 17:48
0

Based on the error:

  

Attempt to read from field 'android.widget.TextView   com.tuinen.carlosvilla.tuinensoft.BandasAdapter $ BandasHolder.text 'on   a null object reference

If there is no ConvertView ( row ), create container from its label using, obtained by getTag() :

if(row == null){ //ConvertView es null.
   ... 
   ...
    row.setTag(holder); //Agrega tag al ConvertView.
} else {
    holder = (BandasHolder) convertView.getTag(); //Crea contenedor a partir de su Etiqueta.
   //o también holder = (BandasHolder) row.getTag();
}
    
answered by 19.12.2016 в 18:14