【问题标题】:Error 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference空对象引用上的错误“void android.widget.TextView.setText(java.lang.CharSequence)”
【发布时间】:2018-10-11 20:52:38
【问题描述】:

我有这个错误

java.lang.NullPointerException: 尝试调用虚方法 'void android.widget.TextView.setText(java.lang.CharSequence)' 为空 对象引用

在这个-holder.Plan.setText(model.getTypeOfPlan());

而且这个 -public 类 padapter 扩展了 FirestoreRecyclerAdapter {

如何解决,希望大家帮忙

这是代码

    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;

    }

}

【问题讨论】:

    标签: java android android-studio nullpointerexception textview


    【解决方案1】:

    我会给你一个笼统的答案,因为这是你在开发 Android/Java 代码时会多次遇到的问题。 (java.lang.NullPointerException)。

    你就是这样解决的;

    1. 确保检查所有 Nullable 项以进行正确初始化。 例如,在您的情况下,不要再猜测哪个对象为空,而是包括检查空值的 if 语句,就像这样,

      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...");
      

    您可以在调试期间使用它,一旦您建立了导致 NullPointerException 的对象,您就可以继续正确初始化它,然后您就可以摆脱 if 语句或检查。

    希望这对现在和将来有所帮助... :)

    【讨论】:

      【解决方案2】:

      我怀疑model.getTypeOfPlan() 正在返回null,在这种情况下,您需要提供更多代码来显示它的来源。

      【讨论】:

        猜你喜欢
        • 2020-02-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-11
        相关资源
        最近更新 更多