为什么我在尝试设置适配器时会收到 nullpointerexception?

Why am i getting a nullpointerexception when I try to setAdapter?

我有一个简单的适配器,它使用 2 个基于字符串的数组列表。出于某种原因,我收到 NullPointerException,即使我确定正在传递信息。

我的适配器:

public class MovieAdapter extends RecyclerView.Adapter<MovieAdapter.ViewHolder> {

    private ArrayList<String> names = new ArrayList<>();
    private ArrayList<String> details = new ArrayList<>();
    private Context mContext;

    public MovieAdapter(ArrayList<String> names, ArrayList<String> details, Context mContext) {
        this.names = names;
        this.details = details;
        this.mContext = mContext;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.detail_view, parent, false);
        ViewHolder holder = new ViewHolder(view);
        return holder;
    }

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


        holder.name.setText(names.get(position));
        holder.detail.setText(details.get(position));
    }

    @Override
    public int getItemCount() {
        return names.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder{

        private TextView name;
        private TextView detail;
        private ConstraintLayout cl;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            name = itemView.findViewById(R.id.name);
            detail = itemView.findViewById(R.id.detial);
            cl = itemView.findViewById(R.id.layout);
        }
    }
}

我的 REST 调用和 recylerView 初始化activity(添加了 REST 调用以防出现问题):

 //        ArrayList<String> namesArray = new ArrayList<>(Arrays.asList("Rated:", "Released:", "Runtime:", "Genre:", , "Writer:", "Actors:", "Plot:", "Language:", "Country:"));

    ArrayList<String> namesArray = new ArrayList<>();


            namesArray.add("Rated:");
            namesArray.add("Released:");
            namesArray.add("Runtime:");
            namesArray.add("Genre:");
            namesArray.add("Director:");
            namesArray.add("Writer:");
            namesArray.add("Actors:");
            namesArray.add("Plot");
            namesArray.add("Language:");
            namesArray.add("Country");


            trazi.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    Call<Movie> call;

                    movieApi = ApiClient.getClient().create(MovieApi.class);

                    if (paramYear.getText().toString().matches("")) {
                        call = movieApi.getDetails(key, paramName.getText().toString(), null);
                    } else {
                        call = movieApi.getDetails(key, paramName.getText().toString(), Integer.parseInt(paramYear.getText().toString()));
                    }


                    call.enqueue(new Callback<Movie>() {
                        @Override
                        public void onResponse(Call<Movie> call, Response<Movie> response) {
                            Toast.makeText(getApplication().getApplicationContext(), " valja", Toast.LENGTH_LONG).show();

                            movie = response.body();


    //                        ArrayList<String> details = new ArrayList<>(Arrays.asList(movie.getRated(), movie.getReleased(), movie.getRuntime(), movie.getGenre(), movie.getDirector(), movie.getWriter(), movie.getActors(), movie.getPlot(), movie.getLanguage(), movie.getCountry()));

                            ArrayList<String> details = new ArrayList<>();
                            details.add(movie.getRated());
                            details.add(movie.getReleased());
                            details.add(movie.getRuntime());
                            details.add(movie.getGenre());
                            details.add(movie.getDirector());
                            details.add(movie.getWriter());
                            details.add(movie.getActors());
                            details.add(movie.getPlot());
                            details.add(movie.getLanguage());
                            details.add(movie.getCountry());

                            Picasso.get().load(movie.getPoster()).fit().centerInside().into(image);
                            image.setVisibility(View.VISIBLE);

                            title.setText(movie.getTitle());
                            title.setVisibility(View.VISIBLE);

                            recyclerView = findViewById(R.id.layout);
                            movieAdapter = new MovieAdapter(namesArray, details, getApplication().getApplicationContext());
                            recyclerView.setAdapter(movieAdapter);
                            recyclerView.setLayoutManager(new LinearLayoutManager(getApplication().getApplicationContext()));

                        }

所以

Picasso.get().load(movie.getPoster()).fit().centerInside().into(image);

title.setText(movie.getTitle());

工作完美,因此信息被传递到适配器,我尝试以两种不同的方式初始化数组列表以防万一。仍然没有成功。关于问题出在哪里的任何想法?

编辑: 我的错误日志:

2020-05-24 17:20:22.600 27595-27595/com.example.cetvrtizadatak E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.cetvrtizadatak, PID: 27595
    java.lang.NullPointerException: Attempt to invoke virtual method 'void androidx.recyclerview.widget.RecyclerView.setAdapter(androidx.recyclerview.widget.RecyclerView$Adapter)' on a null object reference
        at com.example.cetvrtizadatak.MainActivity.onResponse(MainActivity.java:115)
        at retrofit2.DefaultCallAdapterFactory$ExecutorCallbackCall.lambda$onResponse[=14=]$DefaultCallAdapterFactory$ExecutorCallbackCall(DefaultCallAdapterFactory.java:81)
        at retrofit2.-$$Lambda$DefaultCallAdapterFactory$ExecutorCallbackCallwC8FyV4pyjrzrYL5U0mlYiviZw.run(Unknown Source:6)
        at android.os.Handler.handleCallback(Handler.java:873)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:193)
        at android.app.ActivityThread.main(ActivityThread.java:6702)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:911)

当您尝试访问您的 recyclerView 对象的成员时,您的 NullPointerException 发生了,这意味着它的值可能为 null。

查看您的代码,我可以假设您传递的 ID 不是您的 recyclerView,而是布局对象。

仔细检查您的布局 xml 文件中提供给 recyclerView 的 ID 是否是您在调用 findViewById 时引用的 ID。