求大神 android搜索Mysql的内容,int java.util.List.size()错误

安卓开发初学者,本想做一个能搜索数据的小程序,
但是就各种不顺利,帮忙看看哪里出现了错误。
开发环境
Android Studio 3.5.2+XAMPP 3.2.4(Mysql+Apache)+PHP

错误信息

2019-11-29 14:04:26.591 1398-1398/com.uchiyama.livesearch E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.uchiyama.livesearch, PID: 1398
    java.lang.NullPointerException: Attempt to invoke interface method 'int java.util.List.size()' on a null object reference
        at com.uchiyama.livesearch.Adapter.getItemCount(Adapter.java:39)
        at androidx.recyclerview.widget.RecyclerView.dispatchLayoutStep1(RecyclerView.java:4044)
        at androidx.recyclerview.widget.RecyclerView.onMeasure(RecyclerView.java:3534)
        at android.view.View.measure(View.java:19785)
        at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:715)
        at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:461)
        at android.view.View.measure(View.java:19785)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6120)
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
        at androidx.appcompat.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:143)
        at android.view.View.measure(View.java:19785)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6120)
        at androidx.appcompat.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:403)
        at android.view.View.measure(View.java:19785)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6120)
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
        at android.view.View.measure(View.java:19785)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6120)
        at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1464)
        at android.widget.LinearLayout.measureVertical(LinearLayout.java:758)
        at android.widget.LinearLayout.onMeasure(LinearLayout.java:640)
        at android.view.View.measure(View.java:19785)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6120)
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
        at com.android.internal.policy.DecorView.onMeasure(DecorView.java:690)
        at android.view.View.measure(View.java:19785)
        at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2275)
        at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1362)
        at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1611)
        at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1250)
        at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6311)
        at android.view.Choreographer$CallbackRecord.run(Choreographer.java:871)
        at android.view.Choreographer.doCallbacks(Choreographer.java:683)
        at android.view.Choreographer.doFrame(Choreographer.java:619)
        at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:857)
        at android.os.Handler.handleCallback(Handler.java:751)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:241)
        at android.app.ActivityThread.main(ActivityThread.java:6217)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)

我的代码

package com.uchiyama.livesearch;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import java.util.List;

public class Adapter extends RecyclerView.Adapter<Adapter.MyViewHolder> {

    private List<Users> users;
    private Context context;

    public Adapter(List<Users> users, Context context) {
        this.users = users;
        this.context = context;
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view= LayoutInflater.from(parent.getContext()).inflate(R.layout.item,parent,false);
        return new MyViewHolder(view);
    }

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

        holder.name.setText(users.get(position).getName());
        holder.email.setText(users.get(position).getEmail());
    }

    @Override
    public int getItemCount() {
        return users.size(); //在这里报错
    }

   public static class MyViewHolder extends RecyclerView.ViewHolder {
        TextView name,email;

       public MyViewHolder(View itemView) {
           super(itemView);
           name=itemView.findViewById(R.id.name);
           email=itemView.findViewById(R.id.email);
       }
   }

}

2个回答

写的清清楚楚,你的list是null
为什么是null,因为你传了个null
为什么传了个null,数据没查到

a57188731
a57188731 谢谢,我再试试
2 个月之前 回复

检查创建adapter,传入的数据list

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问