Android - listview滚动上的nullPointerException

时间:2012-02-10 19:00:06

标签: android listview

我的自定义列表视图出现问题..

每个视图都有一个单击侦听器,它可以正常工作,除了向下滚动后可以看到的视图外,它在单击其中一个时会抛出nullpointerexception。

如果在滚动之前可以看到这些视图,这些视图实际上是有效的,所以它有点令人困惑。

自定义列表视图的代码如下所示:

public class UserListView extends ArrayAdapter<User>{
int resource;
Context context;
String response;

public UserListView(Context context, int resource, List<User> users){
    super(context,resource,users);
    this.resource=resource;
}

public View getView(int position, View convertView, ViewGroup parent){
    LinearLayout userView;
    User user = getItem(position);  

    userView = new LinearLayout(getContext());
    String inflater = Context.LAYOUT_INFLATER_SERVICE;
    LayoutInflater layoutInflate;
    layoutInflate = (LayoutInflater)getContext().getSystemService(inflater);
    layoutInflate.inflate(resource, userView, true);

    TextView name = (TextView) userView.findViewById(R.id.name);
    RatingBar userRating = (RatingBar) userView.findViewById(R.id.userRatingBar);
    name.setText(String.valueOf(user.getName()));

    float percentageNumber = ((float) user.getTotalQuestionsCorrect() / (float) user.getTotalQuestionsAnswered()) * 100;
    int percentage = (int)percentageNumber;
    setStars(userRating, percentage);
    userView.setId(user.getUserId());
    return userView;
}


private void setStars(RatingBar rb, int percent){
    RatingBar starRating = rb;
    int percentage = percent;

    if(percentage <= 5){starRating.setRating(0);}
    else if(percentage < 25){starRating.setRating(1);}
    else if(percentage >= 25 && percentage < 50){starRating.setRating(2);}
    else if(percentage >= 50 && percentage < 75){starRating.setRating(3);}
    else if(percentage >= 75 && percentage < 100){starRating.setRating(4);}
    else{starRating.setRating(5);}
}

}

任何帮助将不胜感激:)谢谢

2 个答案:

答案 0 :(得分:1)

你的getView()方法中有很多奇怪的东西。我不确定这是问题,但值得一试。第一件事是convertView是一个可以回收的视图,使您不必拥有列表中每个项目的新视图。另一种是当父项传递给getView()方法时,您创建一个LinearLayout作为父视图。第三个奇怪的是,每次需要给视图充气时,你都会得到LayoutInflator。你应该在构造函数中得到它一次,然后坚持下去。试试这个:

public View getView(int position, View convertView, ViewGroup parent){
    User user = getItem(position);  
    if(convertView == null)
    {
        convertView = layoutInflate.inflate(resource, parent, false);
    }

    TextView name = (TextView) convertView.findViewById(R.id.name);
    RatingBar userRating = (RatingBar) convertView.findViewById(R.id.userRatingBar);
    name.setText(String.valueOf(user.getName()));

    float percentageNumber = ((float) user.getTotalQuestionsCorrect() / (float) user.getTotalQuestionsAnswered()) * 100;
    int percentage = (int)percentageNumber;
    setStars(userRating, percentage);
    convertView.setId(user.getUserId());
    return convertView;
}

答案 1 :(得分:0)

覆盖 getCount()方法并返回List的长度。

public class UserListView extends ArrayAdapter<User>{
    int resource;
    Context context;
    String response;
    List<User> users;

    public UserListView(Context context, int resource, List<User> users){
        super(context,resource,users);
        this.resource=resource;
        this.users =users
       }

   @Override
    public int getCount() {
       return users.length;

       }

    }