简体   繁体   中英

String to textview in Android not displaying like it did before within custom adapter

I am using the same custom adapter format that I used in one of my other apps. I only changed the string references but I am getting the following error when running the app:

java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference

I've been trying to wrap my brain around this but I can't figure out what the problem is. Everything is exactly the same as in the other app but for some reason doesn't work in this one. Everything is exactly the same. The only difference is the database table that the data is being drawn from. Here's my getView that is producing the error:

public class Holder
{
    TextView title;
    TextView description;
    NetworkImageView img;
    RatingBar rating;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {

    Holder holder=new Holder();
    View rowView;
    rowView = inflater.inflate(R.layout.product_row, null);
    holder.title=(TextView) rowView.findViewById(R.id.productName);
    holder.img=(NetworkImageView) rowView.findViewById(R.id.productPic);
    holder.rating=(RatingBar)rowView.findViewById(R.id.ratingBar2);
    holder.title.setText(result.get(position).getName());
    holder.rating.setRating(Float.valueOf(result.get(position).getRating().toString()));

    // If you are using NetworkImageView
    holder.img.setImageUrl(result.get(position).getImageURL(), VolleyController.getInstance().getImageLoader());

    return rowView;
}

My row data/model is exactly the same too. The line that is giving the error is:

holder.title.setText(result.get(position).getName());

In my RowData model getName is like this:

public String getName(){
    return name;

Replace yours code with these lines of code . In yours code you are not using the View convertView of getView() method of BaseAdapter instead of it, you used your own View that is rowView ,therefore you are facing these problem.So use the View of the getView() method of adapter class

@Override
public View getView(final int position, View convertView, ViewGroup parent) {

    Holder holder=new Holder();

    if (convertView == null) {

     convertView = inflater.inflate(R.layout.product_row, null);
     holder.title=(TextView) convertView.findViewById(R.id.productName);
     holder.img=(NetworkImageView) convertView.findViewById(R.id.productPic);
     holder.rating=(RatingBar)convertView.findViewById(R.id.ratingBar2);

     convertView.setTag(holder);
   }
   else
   {
       holder = (ViewHolder) convertView.getTag();
   }
       holder.title.setText(result.get(position).getName());
       holder.rating.setRating(Float.valueOf(result.get(position).getRating().toString()));


    // If you are using NetworkImageView
     holder.img.setImageUrl(result.get(position).getImageURL(), VolleyController.getInstance().getImageLoader());

    return convertView;
}

First of all I ask you the question why you used View Holder . because It is the wrong/wrost implementation of view Holder . please read this blog it will help you to understand the view holder pattern https://www.codeofaninja.com/2013/09/android-viewholder-pattern-example.html

And please check the id of TextView in product_row.xml. I thought its just a misspelled id of TestView .

Use the View reference from the getView() do not create separate View .

Use

 `holder.title=(TextView) convertView.findViewById(R.id.productName);`

it may solve the problem

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM