getView中的setText给出Null Pointer Exception

时间:2016-03-10 02:32:33

标签: android baseadapter android-viewholder

我看到与此问题相关的链接很少,但我已经完成了这些链接建议我做的事情。我开始在我的适配器中设置值时,我得到空指针异常。

修改 现在它没有给出空指针异常,但是当它运行时根本没有显示列表。我添加了与之相关的代码。 这是代码

public class EventAdapter extends BaseAdapter {
    Context context;
    LayoutInflater layoutInflater;
    // List<InterestAndLanguageBean> interestAndLanguageBeans=new ArrayList<>();
    List<ReviewBean>arrayList  = new ArrayList<>();

    public EventAdapter(Context context, List<ReviewBean> imagesList) {
        this.context = context;
        this.arrayList = imagesList;
        layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    @Override
    public int getCount() {
        return arrayList.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            holder = new ViewHolder();
            //LayoutInflater inflater = getLayoutInflater();
            convertView = layoutInflater.inflate(R.layout.review_row, parent, false);
            name = (TextView) convertView.findViewById(R.id.name);
            tv_review_text = (TextView) convertView.findViewById(R.id.tv_review_text);
            rb_music = (RatingBar) convertView.findViewById(R.id.rb_music);
            rb_food = (RatingBar) convertView.findViewById(R.id.rb_food);
            rb_price = (RatingBar) convertView.findViewById(R.id.rb_price);
            rb_crowd = (RatingBar) convertView.findViewById(R.id.rb_crowd);
            rated = (TextView) convertView.findViewById(R.id.rate);
            iv_name = (com.pkmmte.view.CircularImageView) convertView.findViewById(R.id.iv_name);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
       holder.name.setText(reviewBeanArrayList.get(position).getUser_name());
        holder.tv_review_text.setText(reviewBeanArrayList.get(position).getComment_text());
        holder.rb_crowd.setRating(Float.parseFloat(reviewBeanArrayList.get(position).getComment_crowd_rating()));
        holder.rb_music.setRating(Float.parseFloat(reviewBeanArrayList.get(position).getComment_music_rating()));
        holder.rb_food.setRating(Float.parseFloat(reviewBeanArrayList.get(position).getComment_food_rating()));
        holder.rb_price.setRating(Float.parseFloat(reviewBeanArrayList.get(position).getComment_price_rating()));
        Glide.with(ActivityAllReviews.this).load(reviewBeanArrayList.get(position).getUser_image()).placeholder(R.drawable.avatr).into(holder.iv_name);
        Float totalRating = (Float.parseFloat(reviewBeanArrayList.get(position).getComment_crowd_rating()) +
                Float.parseFloat(reviewBeanArrayList.get(position).getComment_music_rating()) + Float.parseFloat(reviewBeanArrayList.get(position).getComment_food_rating())
                + Float.parseFloat(reviewBeanArrayList.get(position).getComment_price_rating())) / 4;
        holder.rated.setText(String.valueOf(totalRating));
        return convertView;
    }

}
static class ViewHolder {
    TextView name, tv_review_text,  rated;
    com.pkmmte.view.CircularImageView iv_name;
    RatingBar rb_food,rb_music,rb_price,rb_crowd;
}

行的布局

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#131313"
    android:layout_marginBottom="10dp">
<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">
    <com.pkmmte.view.CircularImageView
        android:layout_width="25dp"
        android:layout_height="25dp"
        android:src="@drawable/avatr"
        android:layout_margin="5dp"
        android:id="@+id/iv_name"/>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Neha Sharma"
        android:textSize="10sp"
        android:id="@+id/name"
        android:textColor="#249D89"
        android:layout_centerVertical="true"
        android:layout_toRightOf="@+id/iv_name"/>
    <ImageView
        android:layout_width="20dp"
        android:layout_height="20dp"
        android:src="@drawable/follow"
        android:layout_centerVertical="true"
        android:layout_toRightOf="@+id/name"
        android:layout_marginLeft="5dp"/>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Rated"
        android:textColor="#ffffff"
        android:textSize="10sp"
        android:layout_gravity="center"
        android:layout_centerVertical="true"
        android:id="@+id/tv_rate"
        android:layout_toLeftOf="@+id/rate"/>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="4.7"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:textColor="#ffffff"
        android:paddingTop="2dp"
        android:paddingBottom="2dp"
        android:paddingLeft="7dp"
        android:paddingRight="7dp"
        android:background="#3B852C"
        android:layout_margin="5dp"
        android:textSize="10sp"
        android:id="@+id/rate"/>
</RelativeLayout>
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/name"
        android:layout_margin="5dp"
        android:textColor="#ffffff"
        android:text="This is the only place on the entire 2nd floor that plays EDM and Dubstep. Has a typical club interior with a trippy ambience and a crowd that's dancing the night away. Not as packed as a Sutra or
Raas, but definitely a place to stop by......"
        android:textSize="10sp"
        android:id="@+id/tv_review_text"/>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_margin="5dp">
        <TextView
            android:layout_width="60dp"
            android:layout_height="wrap_content"
            android:text="Music :"
            android:textColor="#484848"
            android:layout_marginLeft="20dp"/>
        <RatingBar
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            style = "?android:attr/ratingBarStyleSmall"
            android:id="@+id/rb_music"/>
        <TextView
            android:layout_width="60dp"
            android:layout_height="wrap_content"
            android:text="Crowd :"
            android:textColor="#484848"
            android:layout_marginLeft="20dp"/>
        <RatingBar
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            style = "?android:attr/ratingBarStyleSmall"
            android:id="@+id/rb_crowd"/>
        </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_margin="5dp">
        <TextView
            android:layout_width="60dp"
            android:layout_height="wrap_content"
            android:text="Food :"
            android:textColor="#484848"
            android:layout_marginLeft="20dp"/>
        <RatingBar
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            style = "?android:attr/ratingBarStyleSmall"
            android:id="@+id/rb_food"/>
        <TextView
            android:layout_width="60dp"
            android:layout_height="wrap_content"
            android:text="Price :"
            android:textColor="#484848"
            android:layout_marginLeft="20dp"/>
        <RatingBar
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            style = "?android:attr/ratingBarStyleSmall"
            android:id="@+id/rb_price"/>
        </LinearLayout>

</LinearLayout>

活动xml

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
    <include layout="@layout/custom_toolbar"/>

<ListView android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_margin="15dp"
    android:id="@+id/lin_reviews"
    xmlns:android="http://schemas.android.com/apk/res/android" />

    </LinearLayout>

活动onCreate方法

 protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_all_reviews);
    lv_reviews= (ListView) findViewById(R.id.lin_reviews);
    try {
        reviewBeanArrayList = (ArrayList<ReviewBean>) ActivityAllReviews.this.getIntent().getSerializableExtra("reviewlist");
        EventAdapter eventAdapter=new EventAdapter(ActivityAllReviews.this,reviewBeanArrayList);
        lv_reviews.setAdapter(eventAdapter);
    }catch (NullPointerException e)
    {
        e.printStackTrace();
    }

}

1 个答案:

答案 0 :(得分:2)

 name = (TextView) convertView.findViewById(R.id.name); //who is name?
 tv_review_text = (TextView) convertView.findViewById(R.id.tv_review_text);

从我看到的方式来看,它应该是

holder.name = (TextView) convertView.findViewById(R.id.name);
holder.tv_review_text = (TextView) convertView.findViewById(R.id.tv_review_text);

进行编辑。

 List<ReviewBean>arrayList;//= new ArrayList<>(); the rest is not needed
相关问题