简体   繁体   English

为什么我在尝试 setAdapter 时会收到 nullpointerexception?

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

I have a simple adapter that uses 2 String based array lists.我有一个简单的适配器,它使用 2 个基于字符串的数组列表。 For some reason I am getting a NullPointerException even though I am sure the information is being passed by.出于某种原因,即使我确定信息正在传递,我也会收到 NullPointerException。

My adapter:我的适配器:

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);
        }
    }
}

My REST call and recylerView initialization activity(added the REST call in case the problem is lying there):我的 REST 调用和 recylerView 初始化活动(添加了 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()));

                        }

So所以

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

and

title.setText(movie.getTitle());

work perfectly so the information is passed to the adapter and I tried to initialize array lists on two different ways just in case.工作完美,因此信息被传递给适配器,我尝试以两种不同的方式初始化数组列表以防万一。 Still no success.仍然没有成功。 Any ideas on where the problem is?关于问题出在哪里的任何想法?

Edit: My error log:编辑:我的错误日志:

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$1$1.onResponse(MainActivity.java:115)
        at retrofit2.DefaultCallAdapterFactory$ExecutorCallbackCall$1.lambda$onResponse$0$DefaultCallAdapterFactory$ExecutorCallbackCall$1(DefaultCallAdapterFactory.java:81)
        at retrofit2.-$$Lambda$DefaultCallAdapterFactory$ExecutorCallbackCall$1$3wC8FyV4pyjrzrYL5U0mlYiviZw.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)

Your NullPointerException is happening when you try to access a member of you recyclerView object, meaning that its value is probably null.当您尝试访问 recyclerView object 的成员时,会发生 NullPointerException,这意味着它的值可能是 null。

Looking at your code I can presume that the id you're passing is not of yout recyclerView but for an layout object.查看您的代码,我可以假设您传递的 id 不是您的 recyclerView,而是布局 object。

Double check if in your layout xml file the id given to the recyclerView is the one you're referencing on you call to findViewById.仔细检查您的布局 xml 文件中提供给 recyclerView 的 id 是否是您在调用 findViewById 时引用的那个。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 尝试在Android中打开对话框时,为什么会出现NullPointerException? - Why am I getting NullPointerException when I try to open a dialog in Android? 当我尝试在Studio中切换活动时,为什么会收到java.lang.NullPointerException错误? - Why am I getting a java.lang.NullPointerException error when I try to switch activities in studio? 为什么会出现NullPointerException? - Why am I getting NullPointerException? 为什么在添加linearlayout(Android)时会出现nullpointerexception? - Why am I getting a nullpointerexception when adding a linearlayout (Android)? 为什么我在使用moondroid coverflow时会出现NullPointerException? - why am I getting NullPointerException when using moondroid coverflow? 为什么我在ImageView上收到NullPointerException? - Why I am getting NullPointerException on ImageView? 为什么我在这里得到NullPointerException? - Why am I getting NullPointerException here? 为什么在asyncTask中通过AlertDialog收到NullPointerException? - Why am I getting a NullPointerException with AlertDialog in an asyncTask? 为什么在Firebase中的onChildAdded中得到NullPointerException? - Why am I getting NullPointerException in onChildAdded in Firebase? 为什么在此应用程序中出现NullPointerException? - Why am I getting a NullPointerException in this app?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM