2

我正在使用本教程: 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);

它修复了错误,但只有几次我厌倦了模仿 App。我很难完成这项工作,所以我不知道这是正确的修复还是我的代码不好,以后可能会犯更多错误。

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 的片段类:

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

}

还有主要的活动课程,但我认为不需要。

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

4

1 回答 1

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

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

这总是会NullPointerException因为layoutInflateris null 崩溃(这一行是为了分配它)。

由于.from()是静态方法,因此您应该使用:

// Note the capitalization -- I'm using the LayoutInflater class to 
// reference the static method, not the layoutInflater field.
layoutInflater = LayoutInflater.from(context);
于 2016-02-26T04:15:55.327 回答