0
votes

class GetDashboardContentsAysnc extends AsyncTask {

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            shimmerFrameLayout.startShimmer();
        }

        @Override
        protected void onPostExecute(String s) {
            super.onPostExecute(s);
            shimmerFrameLayout.stopShimmer();
            shimmerFrameLayout.setVisibility(View.GONE);
                if(s!=null ) {
                    try {
                        JSONObject jsonObj = new JSONObject(s);
                        response = jsonObj.getString("response");
                        if (response.contains("success")) {

                            listcategories.clear();
                            JSONArray jsonArr = jsonObj.getJSONArray("catgories");
                            for (int i = 0; i < jsonArr.length(); i++) {
                                JSONObject userdata = jsonArr.getJSONObject(i);
                                String category_id = userdata.getString("category_id");
                                String category_name_english = userdata.getString("category_name_english");
                                String app_image = url1 + userdata.getString("app_image");

                                HashMap<String, String> news = new HashMap<>();
                                news.put("category_id", category_id);
                                news.put("category_name_english", category_name_english);
                                news.put("app_image", app_image);

                                listcategories.add(news);

                                Gson gson = new Gson();
                                List<HashMap<String, String>> textList = new ArrayList<>();
                                textList.addAll(listcategories);
                                String jsonText = gson.toJson(textList);
                                session.createdbcategoryesession(jsonText);

                            }


                            JSONArray jsonArr1 = jsonObj.getJSONArray("slider");

                            for (int i = 0; i < jsonArr1.length(); i++) {
                                JSONObject userdata = jsonArr1.getJSONObject(i);
                                String slider_id = userdata.getString("slider_id");
                                String mobile_banner = url + userdata.getString("mobile_banner");
                                ;
                                String slider_url = userdata.getString("slider_url");
                                ;
                                HashMap<String, String> news = new HashMap<>();
                                news.put("slider_id", slider_id);
                                news.put("mobile_banner", mobile_banner);
                                news.put("slider_url", slider_url);
                                listslider.add(news);


                            }


                            listrecipe.clear();
                            JSONArray jsonArr2 = jsonObj.getJSONArray("popular_receipe");

                            for (int i = 0; i < jsonArr2.length(); i++) {
                                JSONObject userdata = jsonArr2.getJSONObject(i);
                                String recipe_id = userdata.getString("recipe_id");
                                String recipe_title = userdata.getString("recipe_title");
                                ;
                                String rating = userdata.getString("rating");
                                String recipe_veg_status = userdata.getString("recipe_veg_status");
                                String image1 = url2 + userdata.getString("image1");

                                HashMap<String, String> news = new HashMap<>();
                                news.put("recipe_id", recipe_id);
                                news.put("recipe_title", recipe_title);
                                news.put("rating", rating);
                                news.put("recipe_veg_status", recipe_veg_status);
                                news.put("image1", image1);
                                listrecipe.add(news);

                                Gson gson = new Gson();
                                List<HashMap<String, String>> textList = new ArrayList<>();
                                textList.addAll(listrecipe);
                                String jsonText = gson.toJson(textList);
                                session.createdbpopularecipesesession(jsonText);


                            }


                            listlatestrecipe.clear();

                            JSONArray jsonArr4 = jsonObj.getJSONArray("latest_recipe");
                            for (int i = 0; i < jsonArr4.length(); i++) {

                                JSONObject userdata = jsonArr4.getJSONObject(i);
                                String recipe_id = userdata.getString("recipe_id");
                                String recipe_title = userdata.getString("recipe_title");
                                ;
                                String rating = userdata.getString("rating");
                                String recipe_veg_status = userdata.getString("recipe_veg_status");
                                String image1 = url2 + userdata.getString("image1");

                                HashMap<String, String> news = new HashMap<>();
                                news.put("recipe_id", recipe_id);
                                news.put("recipe_title", recipe_title);
                                news.put("rating", rating);
                                news.put("recipe_veg_status", recipe_veg_status);
                                news.put("image1", image1);
                                listlatestrecipe.add(news);

                                Gson gson = new Gson();
                                List<HashMap<String, String>> textList = new ArrayList<>();
                                textList.addAll(listlatestrecipe);
                                String jsonText = gson.toJson(textList);
                                session.createdblatestecipesesession(jsonText);
                            }

                        }


                        JSONArray jsonArr3 = jsonObj.getJSONArray("banner");
                        for (int i = 0; i < jsonArr3.length(); i++) {
                            JSONObject userdata = jsonArr3.getJSONObject(i);
                            String section_id = userdata.getString("section_id");
                            String section_image = url4 + userdata.getString("section_image");
                            ;
                            String sectionimage_url_website = userdata.getString("sectionimage_url_website");
                            ;

                            HashMap<String, String> news = new HashMap<>();
                            news.put("section_id", section_id);
                            news.put("section_image", section_image);
                            news.put("sectionimage_url_website", sectionimage_url_website);
                            listbanner.add(news);


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

                }




            lay_slider.setVisibility(View.VISIBLE);
            mPager.setAdapter(new ViewPagerAdaper(getActivity(),listslider));
            indicator.setViewPager(mPager);
            NUM_PAGES =listslider.size();
            final Handler handler = new Handler();
            final Runnable Update = new Runnable() {
                public void run() {
                    if (currentPage == NUM_PAGES)
                    {
                        currentPage = 0;
                    }
                    mPager.setCurrentItem(currentPage++, true);
                }
            };
            Timer swipeTimer = new Timer();
            swipeTimer.schedule(new TimerTask() {
                @Override
                public void run() {
                    handler.post(Update);
                }
            }, 3000, 3000);



            lay_slider1.setVisibility(View.VISIBLE);
            mPager1.setAdapter(new ViewPagerAdaper1(getActivity(),listbanner));
            indicator1.setViewPager(mPager1);
            NUM_PAGES1 =listbanner.size();
            final Handler handler1 = new Handler();
            final Runnable Update1 = new Runnable() {
                public void run() {
                    if (currentPage1 == NUM_PAGES1) {
                        currentPage1 = 0;
                    }
                    mPager1.setCurrentItem(currentPage1++, true);
                }
            };
            Timer swipeTimer1 = new Timer();
            swipeTimer1.schedule(new TimerTask() {
                @Override
                public void run() {
                    handler1.post(Update1);
                }
            }, 3000, 3000);




        }

        RequestHandler requestHandler=new RequestHandler();
        @Override
        protected String doInBackground(String... strings) {
            HashMap<String, String> data = new HashMap<String, String>();
            data.put("customer_id",customer_id);
            data.put("type","2");
            String result = requestHandler.sendPostRequest(getResources().getString(R.string.url_dashboard),data);
            return result;
        }
    }
    GetDashboardContentsAysnc getDashboardContentsAysnc=new GetDashboardContentsAysnc();
    getDashboardContentsAysnc.execute();

E/AndroidRuntime: FATAL EXCEPTION: AsyncTask #4 java.lang.RuntimeException: An error occurred while executing doInBackground() at android.os.AsyncTask$3.done(AsyncTask.java:354) at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:383) at java.util.concurrent.FutureTask.setException(FutureTask.java:252) at java.util.concurrent.FutureTask.run(FutureTask.java:271) at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:245) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641) at java.lang.Thread.run(Thread.java:764) Caused by: java.lang.IllegalStateException: Fragment HomeFragment{c8446f (3220b686-b1ad-479f-ba62-1f4da23bc80a)} not attached to a context. at androidx.fragment.app.Fragment.requireContext(Fragment.java:774) at androidx.fragment.app.Fragment.getResources(Fragment.java:838) at .fragments.HomeFragment$1GetDashboardContentsAysnc.doInBackground(HomeFragment.java:480) at .fragments.HomeFragment$1GetDashboardContentsAysnc.doInBackground(HomeFragment.java:272) at android.os.AsyncTask$2.call(AsyncTask.java:333) at java.util.concurrent.FutureTask.run(FutureTask.java:266) at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:245)  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)  at java.lang.Thread.run(Thread.java:764) 

2

2 Answers

0
votes

As mentioned in the above answer its the fragment visibility issue.

String result = requestHandler
              .sendPostRequest(getResources().getString(R.string.url_dashboard),data);

If you have defined the AsyncTask in side the fragment class and if the fragment is not attached at the time when getResources() method is called the it would cause application to crash. In this case getResources() method would be causing the issue.

Consider doing a visibility check,

String result = "";
if(isVisible())
{
   result = requestHandler
              .sendPostRequest(getResources()
                               .getString(R.string.url_dashboard),data);
}
return result;

This should fix the issue, or else just don't use any method that is provided by the context, like getResouces() instead use static variable or direct URL string there.

String result = requestHandler
              .sendPostRequest("http://api_url_here",data);

Hope this solves you issue. Regards,

0
votes

This is happening because the AsyncTask is trying to update the UI when the current Fragment/Activity is gone in background. This is bound to happen. To avoid this, you can check if current activity/fragment is visible in postExecute method of Asynctask.