错误';void android.widget.TextView.setText(java.lang.CharSequence)和#x27;关于空对象引用
我犯了这个错误 java.lang.NullPointerException:尝试调用虚拟方法“void” 空值上的android.widget.TextView.setText(java.lang.CharSequence)“” 对象引用 在此示例中-holder.Plan.setText(model.getTypeOfPlan()) 这个公共类padapter扩展了FirestoreRecyclerAdapter{ 我该怎么解决呢,希望你们能帮我 这就是代码错误';void android.widget.TextView.setText(java.lang.CharSequence)和#x27;关于空对象引用,java,android,android-studio,nullpointerexception,textview,Java,Android,Android Studio,Nullpointerexception,Textview,我犯了这个错误 java.lang.NullPointerException:尝试调用虚拟方法“void” 空值上的android.widget.TextView.setText(java.lang.CharSequence)“” 对象引用 在此示例中-holder.Plan.setText(model.getTypeOfPlan()) 这个公共类padapter扩展了FirestoreRecyclerAdapter{ 我该怎么解决呢,希望你们能帮我 这就是代码 public class
public class padapter extends FirestoreRecyclerAdapter<addp, padapter.profileHolder> {
private OnItemClickListener listener;
public padapter(@NonNull FirestoreRecyclerOptions<addp> options) {
super(options);
}
@Override
protected void onBindViewHolder(@NonNull profileHolder holder, int position, @NonNull addp model) {
holder.textViewName.setText(model.getName());
holder.Plan.setText(model.getTypeOfPlan());
holder.Price.setText(model.getPrice());
}
@NonNull
@Override
public profileHolder onCreateViewHolder(@NonNull ViewGroup parent, int i) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.profile_item, parent, false);
View Plan = LayoutInflater.from(parent.getContext()).inflate(R.layout.activity_cart, parent, false);
View Price = LayoutInflater.from(parent.getContext()).inflate(R.layout.activity_cart, parent, false);
return new profileHolder(v);
}
class profileHolder extends RecyclerView.ViewHolder {
TextView textViewName;
TextView Plan;
TextView Price;
public profileHolder(@NonNull View itemView) {
super(itemView);
textViewName = itemView.findViewById(R.id.Text_view_name);
Plan = itemView.findViewById(R.id.View_Plan_cart);
Price = itemView.findViewById(R.id.View_Plan_price);
itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
int position = getAdapterPosition();
if(position != RecyclerView.NO_POSITION && listener != null){
listener.onItemClick(getSnapshots().getSnapshot(position),position);
}
}
});
}
}
public interface OnItemClickListener{
void onItemClick(DocumentSnapshot documentSnapshot, int position);
}
public void setOnItemClickListener(OnItemClickListener listener){
this.listener = listener;
}
}
公共类padapter扩展FirestoreRecyclerAdapter{
私人监听;
公共padapter(@NonNull FirestoreRecyclerOptions选项){
超级(期权);
}
@凌驾
受保护的无效onBindViewHolder(@NonNull profileHolder,int位置,@NonNull addp model){
holder.textViewName.setText(model.getName());
holder.Plan.setText(model.getTypeOfPlan());
holder.Price.setText(model.getPrice());
}
@非空
@凌驾
public profileHolder onCreateViewHolder(@NonNull ViewGroup parent,int i){
视图v=LayoutInflater.from(parent.getContext()).flate(R.layout.profile_项,parent,false);
视图平面=LayoutInflater.from(parent.getContext()).flate(R.layout.activity\u cart,parent,false);
查看价格=LayoutInflater.from(parent.getContext()).flate(R.layout.activity\u cart,parent,false);
归还新的档案持有人(v);
}
类profileHolder扩展了RecyclerView.ViewHolder{
TextView textViewName;
文本视图平面图;
文本视图价格;
公共配置文件持有者(@NonNull View itemView){
超级(项目视图);
textViewName=itemView.findViewById(R.id.Text\u view\u name);
Plan=itemView.findViewById(R.id.View\u Plan\u cart);
Price=itemView.findviewbyd(R.id.View\u Plan\u Price);
itemView.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图){
int position=getAdapterPosition();
if(position!=RecyclerView.NO_position&&listener!=null){
onItemClick(getSnapshots().getSnapshot(position),position);
}
}
});
}
}
公共接口侦听器{
void onItemClick(DocumentSnapshot DocumentSnapshot,int位置);
}
公共void setOnItemClickListener(OnItemClickListener侦听器){
this.listener=listener;
}
}
我怀疑model.getTypeOfPlan()
正在返回null
,在这种情况下,您需要提供更多的代码来显示它的来源。我将给您一个概括的答案,因为这是在开发Android/Java代码时您将多次面临的问题。(Java.lang.NullPointerException)
这就是你解决问题的方法
if(holder != null ){
if(model != null){
holder.textViewName.setText(model.getName());
holder.Plan.setText(model.getTypeOfPlan());
holder.Price.setText(model.getPrice());
}else
Log.e(getClass().getSimpleName(),"model object is Null...");
}else
Log.e(getClass().getSimpleName(), "holder object is null...");