如何修复错误'int java.util.List.size()'

时间:2019-07-11 04:17:50

标签: android-studio retrofit adapter


我一直都在一个来源工作!但是现在出现错误

  

int java.util.List.size()


现在我的适配器来源:

public class MessagesAdapter extends RecyclerView.Adapter<MessagesAdapter.msgsViewHoler> {

List<QueryMessages> queryMessages;
Context mtContext;
public MessagesAdapter(List<QueryMessages> queryMessages, MainActivity mainActivity){

    this.queryMessages=queryMessages;

}

@NonNull
@Override
public msgsViewHoler onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

    View view= LayoutInflater.from(parent.getContext()).inflate(R.layout.message_row,parent,false);
    return new msgsViewHoler(view);

}

@Override
public void onBindViewHolder(@NonNull msgsViewHoler holder, int position) {

    final QueryMessages queryMessageses=queryMessages.get(position);
    Picasso.get()
            .load(queryMessageses.getMpic())
            .error(R.mipmap.ic_launcher)
            .fit()
            .into(holder.mpic);
    holder.mname.setText(queryMessageses.getMname());
    holder.message.setText(queryMessageses.getMessage());
    holder.tarikh.setText(queryMessageses.getTarikh());

}

@Override
public int getItemCount() {

    return queryMessages.size();

}

public class msgsViewHoler extends RecyclerView.ViewHolder{

    int id;
    CardView parent;
    ImageView mpic;
    TextView mname,message,tarikh;

    public msgsViewHoler(View itemView) {
        super(itemView);
        parent=itemView.findViewById(R.id.message_row);
        mpic=itemView.findViewById(iv_profile);
        mname=itemView.findViewById(R.id.tv_mastername);
        message=itemView.findViewById(R.id.tv_msg);
        tarikh=itemView.findViewById(R.id.tv_date);
    }
}

并给出此错误:

  

java.lang.NullPointerException:尝试在空对象引用上调用接口方法'int java.util.List.size()'           在ir.arvandit.channeleto.adapter.MessagesAdapter.getItemCount(MessagesAdapter.java:62)

那是一行:

return queryMessages.size();

我的活动主要是:

    RecyclerView rvmsg;
CardView message_row;
List<QueryMessages> queryMessages;
MessagesAdapter adapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    cast();
    onClick();

    ApiService service=ApiClient.getClient().create(ApiService.class);
    Call<List<QueryMessages>> call=service.getQueryMessages();
    call.enqueue(new Callback<List<QueryMessages>>() {
        @Override
        public void onResponse(Call<List<QueryMessages>> call, Response<List<QueryMessages>> response) {

            pros.setVisibility(View.GONE);
            queryMessages=response.body();
            adapter=new MessagesAdapter(queryMessages,MainActivity.this);
            rvmsg.setAdapter(adapter);

        }

        @Override
        public void onFailure(Call<List<QueryMessages>> call, Throwable t) {

            pros.setVisibility(View.GONE);
            nonet.setVisibility(View.VISIBLE);

        }
    });

}

public void cast(){


    rvmsg=findViewById(R.id.rv_message);
    rvmsg.setLayoutManager(new LinearLayoutManager(this,LinearLayoutManager.VERTICAL,false));

}

现在我需要帮助来修复此错误!

2 个答案:

答案 0 :(得分:0)

问题在下面的代码中

 private static final String TAG = "CurrentActivity";

@Override
        public void onResponse(Call<List<QueryMessages>> call, 
          Response<List<QueryMessages>> response) {
            Log.v(TAG, "onResponse=" + response.body().toString());

        }

检查天气response.body()responseArrayList的{​​{1}}

答案 1 :(得分:0)

如果您的json中有free参数,则在新版本中进行改进,会出现此错误
例如我需要id,name,number
但在我的json中有ID,名称,编号,日期...。
我给这个错误! 还有一段时间,如果ApiService中的URL错误!
坦克来帮助所有人