2013-06-05 3 views
0

내 응용 프로그램에서 AsyncTask를 사용을하고 있는데 코드는,java.lang.RuntimeException가 : Looper.prepare()

protected Void doInBackground(Void... params) { 
     // Get the current thread's token 

     try { 
      synchronized (this) { 
       Looper.prepare();     
       if (isCancelled()) { 

       } else {       
        gpsCoordinates = new GetGpsCoordinates(); 
        location = gpsCoordinates.getLocation(context); 

        int counter = 0; 
        while (counter <= 4) { 
         this.wait(850); 
         counter++; 
         publishProgress((int) (counter) * 50); 
        } 
       } 
      } 
     } catch (InterruptedException e) { 
      e.printStackTrace(); 
     } 

     return null; 
    } 

및 GetGpsCoordinates.class와 문제,

public Location getLocation(Context mContext) { 
    try { 
     locationManager = (LocationManager) mContext 
       .getSystemService(LOCATION_SERVICE); 
     Criteria criteria = new Criteria(); 
     criteria.setAccuracy(Criteria.ACCURACY_FINE); 

     // getting GPS status 
     isGPSEnabled = locationManager 
       .isProviderEnabled(LocationManager.GPS_PROVIDER); 

     // getting network status 
     isNetworkEnabled = locationManager 
       .isProviderEnabled(LocationManager.NETWORK_PROVIDER); 

     if (!isGPSEnabled && !isNetworkEnabled) { 
      // no network provider is enabled 
     } else { 
      this.canGetLocation = true; 
      // First get location from Network Provider 
      Log.d("","collecting lat,lng details"); 
      if (isNetworkEnabled) {    
       locationManager.requestLocationUpdates(
         LocationManager.NETWORK_PROVIDER, 
         MIN_TIME_BW_UPDATES, 
         MIN_DISTANCE_CHANGE_FOR_UPDATES, this); 
       Log.d("Network", "Network"); 
       if (locationManager != null) { 
        location = locationManager 
          .getLastKnownLocation(LocationManager.NETWORK_PROVIDER); 
        if (location != null) { 
         latitude = location.getLatitude(); 
         longitude = location.getLongitude(); 
        } 
       } 
      } 
      // if GPS Enabled get lat/long using GPS Services 
      if (isGPSEnabled) { 
       if (location == null) { 
        locationManager.requestLocationUpdates(
          LocationManager.GPS_PROVIDER, 
          MIN_TIME_BW_UPDATES, 
          MIN_DISTANCE_CHANGE_FOR_UPDATES, this); 
        Log.d("GPS Enabled", "GPS Enabled"); 
        if (locationManager != null) { 
         location = locationManager 
           .getLastKnownLocation(LocationManager.GPS_PROVIDER); 
         if (location != null) { 
          latitude = location.getLatitude(); 
          longitude = location.getLongitude(); 

         } 
        } 
       } 
      } 
     } 

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

    return location; 
} 

I looper.prepare()를 사용하면 착취를 얻습니다. 이 내 로그 캣,

06-05 17:26:16.410: E/AndroidRuntime(19075): java.lang.RuntimeException: An error occured while executing doInBackground() 
06-05 17:26:16.410: E/AndroidRuntime(19075): at android.os.AsyncTask$3.done(AsyncTask.java:200) 
06-05 17:26:16.410: E/AndroidRuntime(19075): at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:274) 
06-05 17:26:16.410: E/AndroidRuntime(19075): at java.util.concurrent.FutureTask.setException(FutureTask.java:125) 
06-05 17:26:16.410: E/AndroidRuntime(19075): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:308) 
06-05 17:26:16.410: E/AndroidRuntime(19075): at java.util.concurrent.FutureTask.run(FutureTask.java:138) 
06-05 17:26:16.410: E/AndroidRuntime(19075): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088) 
06-05 17:26:16.410: E/AndroidRuntime(19075): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581) 
06-05 17:26:16.410: E/AndroidRuntime(19075): at java.lang.Thread.run(Thread.java:1019) 
06-05 17:26:16.410: E/AndroidRuntime(19075): Caused by: java.lang.RuntimeException: Only one Looper may be created per thread 
06-05 17:26:16.410: E/AndroidRuntime(19075): at android.os.Looper.prepare(Looper.java:74) 
06-05 17:26:16.410: E/AndroidRuntime(19075): at com.bu.PropertySearchTypes.CameraSearch$LoadViewTask.doInBackground(CameraSearch.java:125) 
06-05 17:26:16.410: E/AndroidRuntime(19075): at com.bu.PropertySearchTypes.CameraSearch$LoadViewTask.doInBackground(CameraSearch.java:1) 
06-05 17:26:16.410: E/AndroidRuntime(19075): at android.os.AsyncTask$2.call(AsyncTask.java:185) 
06-05 17:26:16.410: E/AndroidRuntime(19075): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:306) 

난 스레드가 실행될 때마다 센서의 값 변경이 AsyncTask를 onSensorChanged(), 즉을 부르고있다. 도와주세요. 나는이 예외에 매우 좌절한다. 도움을 주셔서 감사합니다 !!

답변

1

는 새 스레드에서 새로운 루퍼를 시작해야합니다. 예외에는이 정보가 들어 있습니다. 올바른 사용을 위해이 게시물에 체크 아웃 : What is the purpose of Looper and how to use it?

을하지만 진짜 문제는 전혀 필요 여부? 핸들러를 시작하려는 경우 필요합니다. 나는 당신이 그것을 필요로하지만, 심지어 당신이뿐만 아니라 루퍼을 담당 당신을위한 전문 HandlerThread 클래스가, 필요 모르겠어요.