0

this is my fragment class

public class Frangment_electronics extends Fragment {
    private static final String TAG = "RecyclerViewExample";
    private RecyclerView mRecyclerView;
    private RecyclerAdapter adapter;
    private List<Listdetails> listdetails=new ArrayList<Listdetails>();
    private static final String Url = "http://onam.leah.in/new/item_details.php";
    private ProgressDialog progressDialog;

    public Frangment_electronics(){}



    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_electronics, container, false);
        mRecyclerView = (RecyclerView) rootView.findViewById(R.id.recycler_view);

        final LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity());
        mRecyclerView.setLayoutManager(linearLayoutManager);
        adapter = new RecyclerAdapter(getActivity(), listdetails);
        mRecyclerView.setAdapter(adapter);

        RequestQueue queue = Volley.newRequestQueue(getActivity());


        showPD();


        JsonArrayRequest movieReq1 = new JsonArrayRequest(Url,
                new Response.Listener<JSONArray>() {
                    @Override
                    public void onResponse(JSONArray response) {
                        Log.d(TAG, response.toString());
                        hidePD();

                        // Parsing json
                        for (int i = 0; i < response.length(); i++) {
                            try {

                                JSONObject obj = response.getJSONObject(i);

                                 Listdetails item = new Listdetails();
                                 item.setPhone_name(obj.getString("Phone"));
                                item.setPrice(obj.getString("Price"));
                                item.setThumbnail(obj.getString("Image"));



                                listdetails.add(item);

                            } catch (JSONException e) {
                                e.printStackTrace();
                            }

                        }


                        adapter.notifyDataSetChanged();
                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                VolleyLog.d(TAG, "Error: " + error.getMessage());
                hidePD();

            }
        });


        queue.add(movieReq1);
        return rootView;
    }
    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);


    }






    private void showPD() {
        if(progressDialog == null) {
            progressDialog  = new ProgressDialog(getActivity());
            progressDialog .setMessage("Loading...");
            progressDialog .setCancelable(false);
            progressDialog .setCanceledOnTouchOutside(false);
            progressDialog .show();
        }
    }


    private void hidePD() {
        if (progressDialog  != null) {
            progressDialog .dismiss();
            progressDialog  = null;
        }
    }


    @Override
    public void onDestroy() {
        super.onDestroy();
        hidePD();
    }
}

I am getting the following error:

java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.Context android.content.Context.getApplicationContext()' on a null object reference at 
info.androidhive.Groupdeal.app.MySingleton.getRequestQueue(MySingleton.java:61) at info.androidhive.Groupdeal.app.MySingleton.<init>(MySingleton.java:26)
Sander van't Veer
  • 5,930
  • 5
  • 35
  • 50
swaroop
  • 294
  • 1
  • 2
  • 13
  • 1
    can you post the stack trace/ logcat so I can see exactly what the problem is? – Varun Agarwal Oct 13 '15 at 10:11
  • java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.Context android.content.Context.getApplicationContext()' on a null object reference at info.androidhive.Groupdeal.app.MySingleton.getRequestQueue(MySingleton.java:61) at info.androidhive.Groupdeal.app.MySingleton.(MySingleton.java:26) – swaroop Oct 13 '15 at 10:12
  • Sorry Varun i cant post my all code in here its shows some formated errors so that why i post only my fragment here.in my project i wrote listviewholder and adapter class for recyclerview. – swaroop Oct 13 '15 at 10:14
  • Pls read the following http://stackoverflow.com/questions/32678112/error-android-cardview-and-recycler-with-volley/32708983#32708983 – BNK Oct 13 '15 at 10:29

1 Answers1

0
final LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity());
RequestQueue queue = Volley.newRequestQueue(getActivity());

Replace getActivity() with rootView.getContext() in the lines above. There are many other places where you have used getActivity(). Replace them all with rootView.getContext(). In a fragment you cannot call the activity directly, instead you have to use whatever context is associated with the view created by the layout inflater.

EDIT : Change your showPD() code to this

private void showPD(Context context) {
    if(progressDialog == null) {
        progressDialog  = new ProgressDialog(context);
        progressDialog .setMessage("Loading...");
        progressDialog .setCancelable(false);
        progressDialog .setCanceledOnTouchOutside(false);
        progressDialog .show();
    }
}

and call it like this

Context context = rootView.getContext;
showPD(context);
Varun Agarwal
  • 1,587
  • 14
  • 29
  • i replaced all getActivity() with rootView.getContext except the below method private void showPD() { if(progressDialog == null) { progressDialog = new ProgressDialog(getActivity()); progressDialog .setMessage("Loading..."); progressDialog .setCancelable(false); progressDialog .setCanceledOnTouchOutside(false); – swaroop Oct 13 '15 at 10:21
  • you can change this as well. Let me know if it crashes and post the logcat as well. – Varun Agarwal Oct 13 '15 at 10:22
  • i cant change it on the private void showPD() method – swaroop Oct 13 '15 at 10:23
  • added an edit. this will solve your problem. Remember to do this for hidePD as well. – Varun Agarwal Oct 13 '15 at 10:27
  • the app is again crashed and my logcat is java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.Context android.content.Context.getApplicationContext()' on a null object reference at info.androidhive.Groupdeal.app.MySingleton.getRequestQueue(MySingleton.java:61) – swaroop Oct 13 '15 at 10:29
  • while am trying to add Context context = rootView.getContext; showPD(context); inside my oncreateView the .getContext is not resolved – swaroop Oct 13 '15 at 10:36
  • Its the exact same code you used above to replace getActivity() with. if that worked, so should this. Make sure you are calling this AFTER the view inflater. – Varun Agarwal Oct 13 '15 at 10:38
  • i added everything is mentioned above the previous edtion but still it crashed the log cat: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.Context android.content.Context.getApplicationContext()' on a null object reference at info.androidhive.Groupdeal.app.MySingleton.getRequestQueue(MySingleton.java:61) at info.androidhive.Groupdeal.app.MySingleton.(MySingleton.java:26) at info.androidhive.Groupdeal.app.MySingleton.getInstance(MySingleton.java:52) – swaroop Oct 13 '15 at 10:41