簡體   English   中英

為什么我在嘗試 setAdapter 時會收到 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 初始化活動(添加了 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$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)

當您嘗試訪問 recyclerView object 的成員時,會發生 NullPointerException,這意味着它的值可能是 null。

查看您的代碼,我可以假設您傳遞的 id 不是您的 recyclerView,而是布局 object。

仔細檢查您的布局 xml 文件中提供給 recyclerView 的 id 是否是您在調用 findViewById 時引用的那個。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM