当前位置: 首页 > 知识库问答 >
问题:

获取错误尝试在空对象引用上调用虚方法“空android.widget.TextView.setText(java.lang.CharSequence)”

戚良弼
2023-03-14

我试图在android中设置回收器视图,但适配器类给了我一个错误,如标题中所述。我搜索并找到了这个。这里的答案是命名不匹配。我检查了我的名字,但仍然有错误。

这是适配器:

public class RecyclerViewAdapter3 extends RecyclerView.Adapter<RecyclerViewAdapter3.ViewHolder> {

Context context3;
List<ImageUploadInfo> MainImageUploadInfoList;


public RecyclerViewAdapter3(Context context, List<ImageUploadInfo> TempList) {

    this.MainImageUploadInfoList = TempList;

    this.context3 = context;
}

public RecyclerViewAdapter3.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

    View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.recyclerview_items, parent, false);

    ViewHolder viewHolder = new ViewHolder(view);

    return viewHolder;
}

@Override
public int getItemCount() {

    return MainImageUploadInfoList.size();
}

@Override
public void onBindViewHolder(final RecyclerViewAdapter3.ViewHolder holder, int position) {
    ImageUploadInfo UploadInfo = MainImageUploadInfoList.get(position);

    holder.imageNameTextView.setText(UploadInfo.getImageName());

    Uri uri = Uri.parse(UploadInfo.getImageURL());
    Log.d("TAGGG" , ""+uri);
    holder.userText.setText(uri.toString());
}

class ViewHolder extends RecyclerView.ViewHolder {
    public TextView imageNameTextView ;
    public TextView userText;


    public ViewHolder(View itemView) {
        super(itemView);

        userText = (TextView) itemView.findViewById(R.id.textView13);

        imageNameTextView = (TextView) itemView.findViewById(R.id.caption);
    }
}
}

以下是回收器将膨胀的xml文件(项目3):

<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content">

<android.support.v7.widget.CardView
    android:layout_width="303dp"
    android:layout_height="378dp"
    android:layout_marginBottom="8dp"
    app:cardElevation="20dp"
    app:cardCornerRadius="20dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="0.0">

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

        <TextView
            android:id="@+id/textView13"
            android:layout_width="match_parent"
            android:layout_height="327dp"
            android:text="TextView" />

        <TextView
            android:id="@+id/caption"
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:fontFamily="@font/aguafina_script"
            android:gravity="center"
            android:hint="Caption"
            android:textColor="@color/common_google_signin_btn_text_dark_focused"
            android:textSize="26sp" />
    </LinearLayout>
</android.support.v7.widget.CardView>

以下是日志:

Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
    at com.avalonglobalresearch.creatives.RecyclerViewAdapter3.onBindViewHolder(RecyclerViewAdapter3.java:50)
    at com.avalonglobalresearch.creatives.RecyclerViewAdapter3.onBindViewHolder(RecyclerViewAdapter3.java:14)

ImageUploadInfo:

public class ImageUploadInfo {

    public String imageName;

    public String imageURL;

    public ImageUploadInfo() {

    }

    public ImageUploadInfo(String name, String url) {

        this.imageName = name;
        this.imageURL= url;
    }

    public String getImageName() {
        return imageName;
    }

    public String getImageURL() {
        return imageURL;
    }
    }

请告诉我哪里出错了。

共有3个答案

葛玉堂
2023-03-14

在您的< code>onBindViewHolder中,尝试将Holder强制转换为< code>ViewHolder

@Override
public void onBindViewHolder(final RecyclerViewAdapter3.ViewHolder holder, int position) {

    ViewHolder holderView = (ViewHolder)holder; // <-- Cast the ViewHolder

    ImageUploadInfo UploadInfo = MainImageUploadInfoList.get(position);

    holderView.imageNameTextView.setText(UploadInfo.getImageName());  // <-- use holderView instead of holder

    Uri uri = Uri.parse(UploadInfo.getImageURL());
    Log.d("TAGGG" , ""+uri);
    holderView.userText.setText(uri.toString());   // <-- use holderView instead of holder
}
郑狐若
2023-03-14

用这个:

holder.userText.setText(uri.toString() “”);

这可以处理任何数据类型的值

东方和志
2023-03-14

好的,所以我发现了问题,我是膨胀错误的xml文件视图=LayoutInflater.from(parent.getContext())。膨胀(R.layout.recyclerview_items,父,假);这里不是膨胀items3.xml,而是膨胀recyclerview_items,因此我得到了错误。

谢谢大家帮我很抱歉浪费你们的时间

 类似资料: