ListView没有加载,在textview.setText()上返回null对象;

时间:2015-10-15 18:47:55

标签: java android listview

这是我的自定义适配器代码。我收到一个错误,我的textView.setText()为空

"尝试调用虚方法' void android.widget.TextView.setText(java.lang.CharSequence)'在null对象引用"

我通常从我的数据库中检索数据,但是现在我使用硬编码字符串来检查问题是否与数据库有关,但问题仍然存在于硬编码字符串中。

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;
    }
}

这是我的card_view布局(我的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>

我知道这可能是一个愚蠢且易于修复的问题,但我只是不停地敲打头,因为我不知道如何修复它,因为我无法看到我的TextView如何为null

1 个答案:

答案 0 :(得分:1)

您的XML中有一个拼写错误...此行引用了XML中不存在的元素:

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

我认为应该是:

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

基于XML元素:

<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" />