setAdapter 上的空对象引用错误
Posted
技术标签:
【中文标题】setAdapter 上的空对象引用错误【英文标题】:null obejct reference error on setAdapter 【发布时间】:2022-01-15 16:28:50 【问题描述】:我尝试为我的回收站视图设置一个适配器,但它引发了以下错误 尝试在空对象引用上调用虚拟方法 'void androidx.recyclerview.widget.RecyclerView.setAdapter(androidx.recyclerview.widget.RecyclerView$Adapter)'
这是当前活动的代码
package com.example.myapplication;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.os.Bundle;
import android.widget.TextView;
public class resourcetab extends AppCompatActivity
private TextView txt;
private DataModel2 data =new DataModel2();
private RecyclerView recyclerView;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_resourcetab);
data =getIntent().getParcelableExtra("object");
resourceadapter adapter = new resourceadapter(data);
recyclerView.setAdapter(adapter);
回收器视图适配器的代码
package com.example.myapplication;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import java.util.ArrayList;
import java.util.List;
public class resourceadapter extends RecyclerView.Adapter<resourceadapter.ViewHolder>
private List<String> URL= new ArrayList<>();
private List<String> type=new ArrayList<>();
private List<String> Giver=new ArrayList<>();
public resourceadapter(DataModel2 data)
this.URL=data.URL;
this.type=data.type;
this.Giver=data.Giver;
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType)
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.each_resource , parent , false);
return new ViewHolder(view);
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position)
URL.get(position);
holder.type1.setText(type.get(position));
holder.name1.setText(Giver.get(position));
holder.liner.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View v)
);
@Override
public int getItemCount()
return Giver.size();
public class ViewHolder extends RecyclerView.ViewHolder
private LinearLayout liner ;
private TextView type1;
private TextView name1;
public ViewHolder(@NonNull View itemView)
super(itemView);
liner = itemView.findViewById(R.id.liner);
name1 = itemView.findViewById(R.id.textView6);
type1 = itemView.findViewById(R.id.textView7);
【问题讨论】:
您是否已将 View 分配给您的 recyclerview? 私有 RecyclerView recyclerView = findViewById(R.id.your_recyclerview_id); 【参考方案1】:在 onCreate 中设置适配器之前,您必须通过 findViewById 方法初始化您的回收站视图。 喜欢,
recyclerView = findViewById(R.id.recView) //id of Recycler View which you given in your xml
【讨论】:
以上是关于setAdapter 上的空对象引用错误的主要内容,如果未能解决你的问题,请参考以下文章
.getImage() 上的空对象引用 android [重复]
尝试在空对象引用上调用虚方法Spinner.setAdapter(SpinnerAdapter)[duplicate]
在 ListView 中显示两个项目,在 null 对象引用上获取 setAdapter()
Android:尝试在空对象引用上调用虚拟方法“void android.widget.ListView.setAdapter(android.widget.ListAdapter)”
尝试在 recyclerview kotlin 上的空对象引用上调用虚拟方法 'void android.widget.TextView.setText(java.lang.CharSequence)
尝试在空对象引用上调用虚拟方法“void android.support.v4.view.ViewPager.setAdapter(android.support.v4.view.PagerAdapt