android / views-widgets-samples

Multiple samples showing the best practices in views-widgets on Android.
Apache License 2.0
5.05k stars 3.01k forks source link

RecyclerView Causing NullPointer on CardView Item #21

Open troy21688 opened 8 years ago

troy21688 commented 8 years ago

I am using a RecylerView to populate CardView items. I am noticing that the RecyclerView is generating a NullPointer here, as for some reason the RecyclerView references the parent view and not the individual CardView.

I believe the error is occurring here:

  public ViewHolder(View v) {
            super(v);
            userAvatar = (ImageView) findViewById(R.id.profile_image);
            userID = (TextView) findViewById(R.id.user_ID);
            userComment = (TextView) findViewById(R.id.user_comment_textview);
        }

Here is my individual CardView XML (individual_comment.xml):

<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/tools"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="@+id/card_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
card_view:cardCornerRadius="4dp">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/comment_backgroud_grey"
    android:orientation="horizontal"
    android:padding="@dimen/individual_comment_padding">

    <de.hdodenhof.circleimageview.CircleImageView
        android:id="@+id/profile_image"
        android:layout_width="@dimen/avatar_image_dimension"
        android:layout_height="@dimen/avatar_image_dimension"
        android:layout_gravity="center_vertical"
        android:layout_marginLeft="8dp"
        android:layout_marginStart="8dp"
        android:src="@drawable/empty_avatar_256x256"
        app:civ_border_color="#FF000000"
        app:civ_border_width="@dimen/circle_image_border_width" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <TextView
            android:id="@+id/user_ID"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginEnd="16dp"
            android:layout_marginLeft="8dp"
            android:layout_marginRight="16dp"
            android:layout_marginStart="8dp"
            android:layout_marginTop="8dp"
            android:text="Name"
            android:textColor="@color/black"
            android:textSize="@dimen/individual_comment_userID_textsize"
            android:textStyle="bold" />

        <TextView
            android:id="@+id/user_comment_textview"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginEnd="16dp"
            android:layout_marginLeft="8dp"
            android:layout_marginRight="16dp"
            android:layout_marginStart="8dp"
            android:hint="This text should be the comment that I Type, not the default text I set in the XML."
            android:paddingBottom="1dp"
            android:textColor="@color/black"
            android:textSize="@dimen/individual_comment_userComment_textsize" />

    </LinearLayout>
</LinearLayout>

</android.support.v7.widget.CardView>

I am receiving a NullPointer for this line:

 @Override
        public void onBindViewHolder(ViewHolder holder, int position) {
//nullponter, I believe it is not recognizing "user Comment"                holder.userComment.setText(mDataSet.get(position).getUserComment());
                String x = mDataSet.get(position).getUserComment();
                Log.v("ON_BIND_VIEW", "THE STRING IS " + x);
                Log.v("THE_POSITION", "THE POSITION IS " + position);
            }

onCreateViewHolder:

  @Override
    public MyAdapter.ViewHolder onCreateViewHolder(ViewGroup parent,
                                                   int viewType) {
        // create a new view
        View v = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.individual_comment, parent, false);
        // set the view's size, margins, paddings and layout parameters
        return new ViewHolder(v);
    }
ArcMax commented 6 years ago

"RecyclerView references the parent view and not the individual CardView." Ans: Because in viewHolder, each view is not referenced to view v with findViewById.

public ViewHolder(View v) {
            super(v);
            userAvatar = (ImageView)v.findViewById(R.id.profile_image);
            userID = (TextView)v.findViewById(R.id.user_ID);
            userComment = (TextView)v.findViewById(R.id.user_comment_textview);
        }