LayoutInflater 修复 RecyclerView 是正确的解决方案吗?

Was LayoutInflater fix for RecyclerView was correct solution?

我正在使用这个教程: https://www.youtube.com/watch?v=I2eYBtLWGzc

https://mzgreen.github.io/2015/06/23/How-to-hideshow-Toolbar-when-list-is-scrolling(part3)/

起初我遇到错误:

java.lang.NullPointerException: Attempt to invoke virtual method android.view.View android.view.LayoutInflater.inflate(int, android.view.ViewGroup, boolean)' on a null object reference 
    at pl.michalz.hideonscrollexample.adapter.parttwo.MyAdapter.onCreateViewHolder(MyAdapter.java:37)
    at pl.michalz.hideonscrollexample.adapter.parttwo.MyAdapter.onCreateViewHolder(MyAdapter.java:18)

我对以下内容进行了更改:

@Override
public MyAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    Log.w("myApp", "onCreateViewHolder  ");
    View view = layoutInflater.inflate(R.layout.recycler_item, parent, false);
    return new ViewHolder(view);
}

至:

   View view = layoutInflater.from(parent.getContext()).inflate(R.layout.recycler_item, parent, false);

它修复了错误,但只是在几次之后我厌倦了模拟应用程序。我真的很难完成这项工作,所以我不知道这是正确的修复还是我的代码不好,以后可能会出错。

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

    private LayoutInflater layoutInflater;
    private ArrayList<Receptas> receptasL = new ArrayList<>();

    public MyAdapter(Context context) {
        //layoutInflater = layoutInflater.from(context);


    }

    public void setReceptasList(ArrayList<Receptas> receptasL ){
        this.receptasL = receptasL;
        notifyItemRangeChanged(0, receptasL.size());
    }

    @Override
    public MyAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        Log.w("myApp", "onCreateViewHolder  ");
        View view = layoutInflater.from(parent.getContext()).inflate(R.layout.recycler_item, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(MyAdapter.ViewHolder holder, int position) {
        Receptas receptas = receptasL.get(position);
        holder.itemTextViewPav.setText(receptas.getPav());
        holder.itemTextViewIngred_sk.setText(receptas.getIngred_sk());
    }

    @Override
    public int getItemCount() {

        return receptasL == null ? 0 : receptasL.size();
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        TextView itemTextViewPav, itemTextViewIngred_sk;

        public ViewHolder(View itemView) {
            super(itemView);
            itemTextViewPav = (TextView) itemView.findViewById(R.id.itemTextViewPav);
            itemTextViewIngred_sk = (TextView) itemView.findViewById(R.id.itemTextViewIngred_sk);
        }
    }
}

这是用于显示 RecyclerView 的片段 class:

public class PartThreeFragment extends Fragment {

    private final String url = "http://smkbaig.esy.es/get_info_test.php";
    //pridetas
    ArrayList<Receptas> receptasAList = new ArrayList<>();
    MyAdapter myAdapter;
    RecyclerView recyclerView;
    RequestQueue requestQueue;
    Receptas receptas;

    public static PartThreeFragment createInstance() {
        PartThreeFragment partThreeFragment = new PartThreeFragment();
        Log.w("myApp", ">>partThree)");
        return partThreeFragment;
    }

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        Log.w("myApp", "partThree>>");
        View view = inflater.inflate(R.layout.fragment_part_three, container, false);
        recyclerView = (RecyclerView) view.findViewById(R.id.recyclerView);
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        myAdapter = new MyAdapter(getActivity());
        recyclerView.setAdapter(myAdapter);
        requestQueue = Volley.newRequestQueue(this.getActivity());
        volleyJson();
        return view;
    }


    public void volleyJson() {
        JsonObjectRequest jsObjRequest = new JsonObjectRequest
                (Request.Method.GET, url, (String) null, new Response.Listener<JSONObject>() {

                    @Override
                    public void onResponse(JSONObject response) {
                        try {

                            JSONArray ja = response.getJSONArray("receptai");
                            for (int i = 0; i < ja.length(); i++) {

                                JSONObject jsonObject = ja.getJSONObject(i);
                                receptas = new Receptas();
                                receptas.setPav(jsonObject.getString("pav"));
                                //receptas.setApras(jsonObject.getString("apras"));
                                receptas.setIngred_sk(jsonObject.getString("ingred_sk"));
                                receptasAList.add(receptas);
                                Log.w("myApp", "receptasAList.add(receptas); " + receptasAList.get(0).getPav());
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                        //atliekamas po getRecceptas()
                        Log.w("myApp", "myAdapter.setReceptasList(receptasAList);");
                        myAdapter.setReceptasList(receptasAList);
//                         myAdapter.notifyDataSetChanged();
                        //mTextView.setText("Response: " + response.toString());
                    }
                },
                        new Response.ErrorListener() {
                            @Override
                            public void onErrorResponse(VolleyError error) {
                                // TODO Auto-generated method stub
                            }
                        }
                );
        requestQueue.add(jsObjRequest);
    }

}

还有mainactivityclass不过我觉得没必要

如果这个问题不正确,我不知道还能怎么问,但我非常感谢任何帮助。

public MyAdapter(Context context) {
    //layoutInflater = layoutInflater.from(context);
}

您在这里混淆了静态方法和实例方法,这是您的问题。此时,字段 layoutInflater 为空。然后您尝试取消引用它并对其调用 .from() (这是一个静态方法)。

这总是会因 NullPointerException 而崩溃,因为 layoutInflater 为空(此行用于分配它)。

由于 .from() 是一个静态方法,您应该改为使用:

// Note the capitalization -- I'm using the LayoutInflater class to 
// reference the static method, not the layoutInflater field.
layoutInflater = LayoutInflater.from(context);