2013-07-10 1 views
-1

아래 메서드는 AsynTask의 여러 위치에서 호출되고 있습니다. Android 용으로 코딩되었습니다. ConcurrentModificationException을 가져 오는 중입니다.ConcurrentModificationException

private final Object lock = new Object(); 

public static String saveJsonFile(File dir, String name, JSONObject data) { 
    final File file = new File(dir.getPath() + File.separator + name); 
    try { 
     file.createNewFile(); 
    } catch (final IOException e1) { 
     // TODO Auto-generated catch block 
     e1.printStackTrace(); 
    } 

    BufferedWriter printWriter = null; 

    try { 
     printWriter = new BufferedWriter(new FileWriter(file), 8192); 

     synchronized(lock) { 
      if (data != null) 
       data.write(printWriter); // java.util.ConcurrentModificationException 
       //... 
+1

이 JSONObject.write입니다()? http://developer.android.com/reference/org/json/JSONObject.html에 도착하지 않았습니다. –

+0

파일에 데이터를 쓰는 데 추가 된 사용자 정의 방법입니다. 나는 Jar 파일로 컴파일 된 것을 볼 수 없다. 실제로 그것은 org.json.douglascrockford.JSONObject입니다. –

+0

@ Vinayak.B 그 클래스에는 기본 맵을 변경하는 두 가지 메소드 (다른 생성자)가 있습니다. JSONObject에'put' 또는'popuplate'를 호출 한 적이 있습니까? –

답변

0

안전이 방법 스레드 당신은 같은 것을 할 수 있도록하는

synchronized(data) { 
    try { 
     printWriter = new BufferedWriter(new FileWriter(file), 8192); 
     // ... 
    } 
} 
0

이 코드 synchronizeddata에서 확인하십시오 : 어떻게

public static String saveJsonFile(File dir, String name, JSONObject data) { 
     final File file = new File(dir.getPath() + File.separator + name); 
     try { 
      file.createNewFile(); 
     } catch (final IOException e1) { 
      // TODO Auto-generated catch block 
      e1.printStackTrace(); 
     } 

     BufferedWriter printWriter = null; 

     try { 
      printWriter = new BufferedWriter(new FileWriter(file), 8192); 
      // printWriter = new PrintWriter(file); 
      if (data != null) 
       data.write(printWriter); // java.util.ConcurrentModificationException 



         ........ 
         ........ 
+0

행운이 없습니다. 이 같은 예외가 점점 –

관련 문제