求大神 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 谢谢,我再试试
大约一个月之前 回复

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

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

相似问题

2
Random命令一直报错,java.util包中没有Random类?
1
格式化日期在8.0手机报java.lang.AssertionError
2
java.sql.SQLException: No value specified for parameter 5
2
Runnable 中抛出java.lang.ArrayIndexOutOfBoundsException: -1
1
AndroidStudio 编程中出现java.lang.RuntimeException错误
2
多线程数据安全问题,为啥会爆java.util.NoSuchElementException
3
在Java中可以把e.printStackTrace()的信息通过java.util.logging.Logger来记录下来吗?
2
idea编译出错 java.io.IOException: Cannot create empty file:
2
多线程运行,cpu使用率过高
2
war包部署到tomcat启动错误 求大神支招(本人不会java)
3
运行报错,大佬们帮忙看看什么原因java.util.concurrent.ExecutionException
4
Could not load driverClass com.mysql.jbdc.Driver,配置好jar了还是找不到
5
Exception in thread "Thread-12" java.lang.RuntimeException: java.lang.NullPointerException
3
为什么在try外面声明Scanner对象,只能在try中使用一次,第二次就不能用了,try在一个死循环中
2
android APP连接mysql时,APP闪退
1
Android Studio 升级到3.5的错误
2
淘淘商城在构建mybatis逆向工程报错:com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException
1
AS创建新项目报错java.util.concurrent.ExecutionException: org.apache.http.conn.ConnectTimeoutException
1
java中mysql的异常 ,求助
0
springboot 项目在本地启动正常,maven打包运行包如下错误,在项目中去掉 @KafkaListener注解打包运行不会报错,怀疑是jar依赖的问题,求解!