简体   繁体   中英

ListView not loading, returns null object on textview.setText();

Here is my custom adapter code. I get an error that my textView.setText() is null

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

I usually retrieve data from my database, but right now I used hardcoded string just to check if the problem is with the database, but the problem persist with the hardcoded string.

public class CardViewAdapter extends BaseAdapter {

    Context context;
    List<Note> notes;

    TextView cardTitleText;
    TextView cardDescriptionText;
    TextView cardDateText;
    ImageView cardImage;

    public CardViewAdapter(Context context, ArrayList<Note> notes) {
        this.context = context;
        this.notes = notes;
    }

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

    @Override
    public Object getItem(int position) {
        return notes.get(position);
    }

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

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

        LayoutInflater inflater = (LayoutInflater) context.getSystemService(LAYOUT_INFLATER_SERVICE);
        View view = inflater.inflate(R.layout.card_view, parent, false);

        cardTitleText = (TextView) view.findViewById(R.id.titleTextCardView);
        cardDescriptionText = (TextView) view.findViewById(R.id.descriptionTextView);
        cardDateText = (TextView) view.findViewById(R.id.dateTextCardView);
        cardImage = (ImageView) view.findViewById(R.id.cardImage);

        Note note = notes.get(position);
        Log.v("NOTE", note.getTitle() + " " + note.getDescription() + " " + note.getDate());

        cardTitleText.setText("Title");
        cardDescriptionText.setText("Description");
        cardDateText.setText("Date");

        return view;
    }
}

Here is my card_view layout (item layout for my listView):

<RelativeLayout
android:id="@+id/relativeLayoutCardView"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:background="@drawable/relative_layout"
android:elevation="6dp"
xmlns:android="http://schemas.android.com/apk/res/android" >

<TextView
    android:id="@+id/titleTextCardView"
    android:layout_below="@+id/frameLayoutCardView"
    android:layout_centerHorizontal="true"
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:paddingTop="10dp"
    android:text="Example Title"
    android:textSize="20sp"
    android:textStyle="bold" />

<TextView
    android:id="@+id/descriptionTextCardView"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/titleTextCardView"
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:paddingLeft="5dp"
    android:paddingTop="5dp"
    android:text="Example description"
    android:textSize="13sp"
    android:textStyle="bold" />

<FrameLayout
    android:id="@+id/frameLayoutCardView"
    android:layout_height="130dp"
    android:layout_width="fill_parent">

    <ImageView
        android:id="@+id/cardImage"
        android:layout_height="130dp"
        android:layout_width="fill_parent"
        android:scaleType="fitXY"
        android:src="@drawable/placeholder" />

    <TextView
        android:id="@+id/dateTextCardView"
        android:layout_gravity="center_horizontal"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:text="Example date" />
</FrameLayout> </RelativeLayout>

I know that this is probably a stupid and easy to fix problem, but I just keep banging my head because I don't know how to fix it because I can't see how can my TextView be null.

You have a typo in your XML... this line references an element that does not exist in your XML:

cardDescriptionText = (TextView) view.findViewById(R.id.descriptionTextView);

I think it should be:

cardDescriptionText = (TextView) view.findViewById(R.id.descriptionTextCardView);

based on the XML element:

<TextView
    android:id="@+id/descriptionTextCardView"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/titleTextCardView"
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:paddingLeft="5dp"
    android:paddingTop="5dp"
    android:text="Example description"
    android:textSize="13sp"
    android:textStyle="bold" />

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