android - RuntimeException Can't create handler inside thread that has not called Looper.prepare() -


i'm trying make volley request when receive localbroadcast message. i'm getting runtimeexception. tried using thread still getting same error. should use else? i'm getting exception in method preparedata() that's why added try/catch inside it. in try/catch i'm getting exception.

public class newitemsnearufragment extends fragment{        private sharedpref pref;      private connectiondetector cd;      private progressdialog pd;      private localbroadcastmanager bmanager;      activity activity;        @override      public void onviewcreated(view view, @nullable bundle savedinstancestate) {          super.onviewcreated(view, savedinstancestate);            pref = new sharedpref(getactivity());          cd = new connectiondetector(getactivity());          pd = new progressdialog(getactivity());            bmanager = localbroadcastmanager.getinstance(getactivity());          intentfilter intentfilter = new intentfilter();          intentfilter.addaction("update_notify");          bmanager.registerreceiver(breceiver, intentfilter);        }          @override      public void ondestroy() {          localbroadcastmanager.getinstance(activity).unregisterreceiver(breceiver);          super.ondestroy();      }          @override      public void onattach(context context) {          super.onattach(context);            if (context instanceof activity){              activity=(activity) context;          }        }        private broadcastreceiver breceiver = new broadcastreceiver() {      	@override          public void onreceive(context context, intent intent) {              if(intent!=null){                  if(intent.getaction().equals("update_notify")) {                      system.out.println("sammy_received_broadcast");                      new thread() {                          public void run() {                                    try {                                      activity.runonuithread(new runnable() {                                            @override                                          public void run() {                                                                                              preparedata();                                          }                                      });                                      thread.sleep(300);                                  } catch (interruptedexception e) {                                      e.printstacktrace();                                  }                            }                      }.start();                      }              }            }      };          private void preparedata(){            try{                pd.setmessage(activity.getstring(r.string.plzwait));              pd.show();                if(arraylist!=null )arraylist.clear();                if(textutils.isempty(pref.getstring(utility.home_lat)))                  pref.setstring(utility.home_lat, string.valueof(gpstracker.currlat));                if(textutils.isempty(pref.getstring(utility.home_lang)))                  pref.setstring(utility.home_lang, string.valueof(gpstracker.currlong));                if(textutils.isempty(pref.getstring(utility.home_priceorder)))                  pref.setstring(utility.home_priceorder, "");                if(textutils.isempty(pref.getstring(utility.home_dataorder)))                  pref.setstring(utility.home_dataorder, "");                if(textutils.isempty(pref.getstring(utility.home_prodname)))                  pref.setstring(utility.home_prodname, "");                if(textutils.isempty(pref.getstring(utility.home_catid)))                  pref.setstring(utility.home_catid, "");                if(textutils.isempty(pref.getstring(utility.home_startprice)))                  pref.setstring(utility.home_startprice, "");                if(textutils.isempty(pref.getstring(utility.home_endprice)))                  pref.setstring(utility.home_endprice, "");                  stringrequest stringrequest = new stringrequest(request.method.post, utility.allprod,                      new response.listener<string>() {                          @override                          public void onresponse(string response) {                              pd.dismiss();                              system.out.println("sammy_all_prod_response "+response);                                try {                                  jsonobject jobj = new jsonobject(response);                                  if(jobj.getint("ack")==1){                                      jsonarray jsonarray = jobj.getjsonarray("all_products");                                      for(int i=0;i<jsonarray.length();i++){                                          jsonobject main = jsonarray.getjsonobject(i);                                            model home = new model();                                          home.setuserid(main.getstring("user_id"));                                          home.settitle(main.getstring("name"));                                          string price = "$"+main.getstring("price");                                          home.setprice(price);                                          home.setimage(main.getstring("image"));                                          home.setprdid(main.getstring("product_id"));                                            arraylist.add(home);                                        }                                  }                                    adapter.notifydatasetchanged();                                  } catch (jsonexception e) {                                  // json error                                  e.printstacktrace();                              }                            }                      }, new response.errorlistener() {                  @override                  public void onerrorresponse(volleyerror error) {                      pd.dismiss();                      if (error instanceof timeouterror) {                          toast.maketext(getactivity(),getstring(r.string.tooslow),toast.length_long).show();                      }else if (error instanceof noconnectionerror){                          toast.maketext(getactivity(),getstring(r.string.nointernet),toast.length_long).show();                      }else if (error instanceof authfailureerror) {                          system.out.println("sammy_authfailureerror "+error);                      } else if (error instanceof servererror) {                          system.out.println("sammy_servererror "+error);                      } else if (error instanceof networkerror) {                          system.out.println("sammy_networkerror "+error);                      } else if (error instanceof parseerror) {                          system.out.println("sammy_parseerror "+error);                      }                  }              }) {                  @override                  protected map<string, string> getparams() {                      map<string, string> params = new hashmap<string, string>();                      params.put("user_id", pref.getstring(utility.userid));                      params.put("user_lat", pref.getstring(utility.home_lat));                      params.put("user_lang", pref.getstring(utility.home_lang));                      params.put("price_order", pref.getstring(utility.home_priceorder));                      params.put("data_order", pref.getstring(utility.home_dataorder));                      params.put("product_name", pref.getstring(utility.home_prodname));                      params.put("categoryid", pref.getstring(utility.home_catid));                      params.put("start_price", pref.getstring(utility.home_startprice));                      params.put("end_price", pref.getstring(utility.home_endprice));                          system.out.println("sammy_all_prod_params "+params);                      return params;                  }                };                  requestqueue requestqueue = volley.newrequestqueue(activity.getapplicationcontext());              //if(getactivity()!=null)              requestqueue.add(stringrequest);              stringrequest.setretrypolicy(new defaultretrypolicy(                      10000,                      defaultretrypolicy.default_max_retries,                      defaultretrypolicy.default_backoff_mult));              }catch(exception e){              system.out.println("sammy_preparedata_exception "+e);          }          }    }

your mistake calling preparedata(); inside thread

where in preparedata() function doing ui operation. showing toast , progressdialog etc.

toast needs activity show user interface , threads don't have that.

you should use ui thread instead:

 getactivity().runonuithread(new runnable() {     public void run() {         toast.maketext(getactivity(), "message", toast.length_long).show();     } }); 

Comments

Popular posts from this blog

javascript - Clear button on addentry page doesn't work -

c# - Selenium Authentication Popup preventing driver close or quit -

tensorflow when input_data MNIST_data , zlib.error: Error -3 while decompressing: invalid block type -