Java android java.util.ConcurrentModificationException -


i have service in have asynctask

if (s != null) {                     if (!mainactivity.photolistsend.isempty()) {                         if (mainactivity.photolistsend.size() > 0) {                             file file = mainactivity.photolistsend.get(0);                             if (file.exists())                                 file.delete();                             mainactivity.photolistsend.remove(0);                             gson gson = new gson();                             string jsoncurproduct = gson.tojson(mainactivity.photolistsend);                             sharedpreferences sharedpref = getapplicationcontext().getsharedpreferences("tag", context.mode_private);                             sharedpreferences.editor editor = sharedpref.edit();                             editor.putstring("tag", jsoncurproduct);                             editor.apply();                             file imagesfolder = context.getexternalfilesdir(environment.directory_pictures);                             assert imagesfolder != null;                         }                     }                  } 

on line :

string jsoncurproduct = gson.tojson(mainactivity.photolistsend); 

i have java.util.concurrentmodificationexception

and class :

ublic class sendrer extends service {      public static boolean running = false;     private timer timer = new timer();     private sendphototask asyncsender;     private context context;     private sharedpreferences sp;     private sharedpreferences.editor editor;      public static string convertstreamtostring(java.io.inputstream is) {         java.util.scanner s = new java.util.scanner(is).usedelimiter("\\a");         return s.hasnext() ? s.next() : "";     }      @override     public void oncreate() {         super.oncreate();         context = getapplicationcontext();         sp = getsharedpreferences("pfref", activity.mode_private);         editor = sp.edit();         gson gson = new gson();         list<file> productfromshared = new arraylist<>();         sharedpreferences sharedpref = getapplicationcontext().getsharedpreferences("tag", context.mode_private);         string jsonpreferences = sharedpref.getstring("tag", "");          type type = new typetoken<list<file>>() {         }.gettype();         productfromshared = gson.fromjson(jsonpreferences, type);         mainactivity.photolistsend = null;         mainactivity.photolistsend = new arraylist<>();         if (productfromshared != null)             mainactivity.photolistsend.addall(productfromshared);         log.e("tworzenie serwisu ", "tworzenie");     }      @override     public int onstartcommand(intent intent, int flags, int startid) {         log.e("dziełanie serwisu ", "dziełanie");          if (!running) {             running = true;             handler handler = new handler();             handler.postdelayed(new runnable() {                 @override                 public void run() {                     asyncsender = new sendphototask();                     asyncsender.execute();                 }             }, 1000 * 60 * 2);         }          return start_sticky;     }      @override     public void ondestroy() {         if (running) {             timer.cancel();             asyncsender = new sendphototask();             asyncsender.cancel(true);             running = false;         }         log.e("service ", "nie działa");         super.ondestroy();     }      @nullable     @override     public ibinder onbind(intent intent) {         return null;     }      public boolean ismyservicerunning(class<?> serviceclass) {         activitymanager manager = (activitymanager) getsystemservice(context.activity_service);         (activitymanager.runningserviceinfo service : manager.getrunningservices(integer.max_value)) {             if (serviceclass.getname().equals(service.service.getclassname())) {                 return true;             }         }         return false;     }      class sendphototask extends asynctask<string, void, string> {          @override         protected string doinbackground(string... strings) {             running = true;             if (mainactivity.photolistsend != null) {                 if (!mainactivity.photolistsend.isempty())                     if (networkutil.isnetworkavailable(context)) {                         if (mainactivity.photolistsend.size() > 0) {                             mainactivity.issend = true;                             running = true;                             inputstream responseinputstream = null;                             log.e("start wysłania ", "start");                             try {                                 if (mainactivity.photolistsend.get(0).isfile()) {                                     responseinputstream = httpconnectionsutil.sendphotorequest(getapplicationcontext(), true, mainactivity.photolistsend.get(0).getname());                                     if (responseinputstream != null) {                                         string input = convertstreamtostring(responseinputstream);                                         if (input.equals("empty"))                                             return "bad";                                         else {                                             try {                                                 int tt = responseparser.gettype(input);                                                 log.e("tag", tt + " ");                                                  if (tt == 0) {                                                     return null;                                                 } else if (tt == -1) {                                                     return null;                                                 }                                             } catch (unknownanswername e) {                                                 e.printstacktrace();                                                 return null;                                             }                                         }                                     }                                  } else {                                     return "bad";                                 }                              } catch (ioexception e) {                                 e.printstacktrace();                                 return null;                             } //                    log.e("wysyłanie zdjęcia ", convertstreamtostring(responseinputstream));                             if (responseinputstream != null)                                 return convertstreamtostring(responseinputstream);                         }                     }             }             return null;         }          @override         protected void onpostexecute(string s) {             super.onpostexecute(s);             if (networkutil.isnetworkavailable(context)) {                 if (sp.getboolean("workoffline", false)) {                     editor.putboolean("workoffline", false);                     editor.commit();                     isconnect.setvisibility(view.visible);                     imgissend.setvisibility(view.visible);                     imgisnet.setvisibility(view.gone);                 }             }             if (s != null) {                 if (!mainactivity.photolistsend.isempty()) {                     if (mainactivity.photolistsend.size() > 0) {                         file file = mainactivity.photolistsend.get(0);                         if (file.exists())                             file.delete();                         mainactivity.photolistsend.remove(0);                         gson gson = new gson();                         string jsoncurproduct = gson.tojson(mainactivity.photolistsend);                         sharedpreferences sharedpref = getapplicationcontext().getsharedpreferences("tag", context.mode_private);                         sharedpreferences.editor editor = sharedpref.edit();                         editor.putstring("tag", jsoncurproduct);                         editor.apply();                         file imagesfolder = context.getexternalfilesdir(environment.directory_pictures);                         assert imagesfolder != null;                     }                 }              }             if (s == null) {                 mainactivity.issend = false;             }             if (!mainactivity.photolistsend.isempty()) {                 if (networkutil.isnetworkavailable(context)) {                     if (mainactivity.photolistsend.size() > 0) {                         asyncsender = new sendphototask();                         asyncsender.execute();                         log.e("wysyłanie kolejnego ", "zdjecia");                     } else {                         context.stopservice(new intent(context, sendrer.class));                         asyncsender.cancel(true);                         context.startservice(new intent(context, sendrer.class));                     }                  } else {                     context.stopservice(new intent(context, sendrer.class));                     asyncsender.cancel(true);                     context.startservice(new intent(context, sendrer.class));                 }             } else {                 mainactivity.issend = false;                 context.stopservice(new intent(context, sendrer.class));                 asyncsender.cancel(true);                 context.startservice(new intent(context, sendrer.class));             }             running = false;         }     } } 

that's because thread modifying mainactivity meanwhile. can branch line with

synchronized(mainactivity.this) {     string jsoncurproduct = gson.tojson(mainactivity.photolistsend); } 

however should aware of possible performance issues because threads waiting till tojson method ends

read more exception , how avoid this


Comments

Popular posts from this blog

python - Selenium remoteWebDriver (& SauceLabs) Firefox moseMoveTo action exception -

html - How to custom Bootstrap grid height? -

transpose - Maple isnt executing function but prints function term -