2012-04-17 91 views
0

任何人都可以幫我弄清楚爲什麼有時AsyncTask GeoCoder SOMETIMES的工作原理和其他時候會崩潰?我瀏覽了一些與這個主題相關的帖子,但似乎我可以與它相關。AsyncTask GeoCoder有時會崩潰?

繼承人mycode的:

public class statuspage extends MapActivity { 

LocationManager locationManager; 
MapView mapView; 
Criteria criteria; 
Location location; 
Geocoder gc; 
Address address; 

String bestProvider; 
String LOCATION_SERVICE = "location"; 
String addressString = "No address found"; 
String lonLat = "lonLat"; 
StringBuilder sb; 
StringBuilder ssb; 

private MapController mapController; 
private MyLocationOverlay myLocation; 

public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.statuspage); 

    // Get Mapping Controllers etc // 
    mapView = (MapView) findViewById(R.id.mapView); 
    mapController = mapView.getController(); 
    mapController.setZoom(17); 
    mapView.setBuiltInZoomControls(true); 

    // Add the MyLocationOverlay // 
    myLocation = new MyLocationOverlay(this, mapView); 
    mapView.getOverlays().add(myLocation); 
    myLocation.enableMyLocation(); 

    // Animates the map to GPS Position // 
    myLocation.runOnFirstFix(new Runnable() { 
     public void run() { 
      mapController.animateTo(myLocation.getMyLocation()); 
     } 
    }); 
} 

@Override 
protected boolean isRouteDisplayed() { 

    // Executes GeoCoding AsyncTask // 
    new GeoCoder().execute(); 

    // Location Manager Intiation 
    locationManager = (LocationManager) statuspage.this 
      .getSystemService(LOCATION_SERVICE); 
    criteria = new Criteria(); 

    // More accurate, GPS fix. 
    criteria.setAccuracy(Criteria.ACCURACY_FINE); // More accurate, GPS fix. 
    bestProvider = locationManager.getBestProvider(criteria, true); 
    location = locationManager.getLastKnownLocation(bestProvider); 
    if (location != null) { 

     // Latitude and Longitude TextView 
     TextView etlongitude = (TextView) findViewById(R.id.etlongitude); 
     TextView etlatitude = (TextView) findViewById(R.id.etlatitude); 

     double latitude = location.getLatitude(); 
     double longitude = location.getLongitude(); 

     // Latitude and Longitude TextView Display Coordinates // 
     etlongitude.setText((longitude) + ""); 
     etlatitude.setText((latitude) + ""); 

     // locationManager.removeUpdates((LocationListener) location); 
     locationManager = null; 

    } else { 

    } 

    return false; 
} 

class GeoCoder extends AsyncTask<Void, Void, Void> { 

    @Override 
    protected Void doInBackground(Void... params) { 
     Log.d("AsyncTask", "GeoCoder-doInBackGround"); 

     double latitude = location.getLatitude(); 
     double longitude = location.getLongitude(); 

     gc = new Geocoder(statuspage.this, Locale.getDefault()); 
     try { 

      List<Address> addresses = gc.getFromLocation(latitude, 
        longitude, 1); 

      sb = new StringBuilder(); 
      if (addresses != null && addresses.size() > 0) { 
       address = addresses.get(0); 
       int noOfMaxAddressLine = address.getMaxAddressLineIndex(); 
       if (noOfMaxAddressLine > 0) { 
        for (int i = 0; i < address.getMaxAddressLineIndex(); i++) { 
         sb.append(address.getAddressLine(i)).append("\n"); 
        } 
        addressString = sb.toString(); 
       } 
      } 
     } catch (Exception e) { 
      addressString = e.toString(); 
     } 

     return null; 
    } 

    @Override 
    protected void onPostExecute(Void result) { 

     TextView scrollview = (TextView) findViewById(R.id.scrollview); 
     scrollview.setText("Current Position:" + "\n" 
       + "(Accurate to 500 meters)" + "\n" + (addressString)); 

     Log.d("AsyncTask", "GeoCoder-onPostExecute"); 
     return; 
    } 

} 

@Override 
protected void onResume() { 
    super.onResume(); 
    myLocation.enableMyLocation(); 
    //new GeoCoder().execute(); 
} 

@Override 
protected void onPause() { 
    super.onPause(); 
    myLocation.disableMyLocation(); 
    //new GeoCoder().cancel(true); 
} 

@Override 
public void onBackPressed() { 

    // Button OnClick Vibrating Service 
    Vibrator vib = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE); 

    // Vibrate Service 
    vib.vibrate(50); 

    startActivity(new Intent(statuspage.this, AgentPortalActivity.class)); 
    statuspage.this.finish(); 

    /** Fading Transition Effect */ 
    overridePendingTransition(R.anim.fadein, R.anim.fadeout); 

    return; 

} 
} 

這裏是我的日誌:

04-18 08:36:42.812: I/Maps.MyLocationOverlay(18136): Request updates from gps 
04-18 08:36:42.812: I/Maps.MyLocationOverlay(18136): Request updates from network 
04-18 08:36:42.859: I/MapActivity(18136): Handling network change notification:CONNECTED 
04-18 08:36:42.859: E/MapActivity(18136): Couldn't get connection factory client 
04-18 08:36:42.910: D/AsyncTask(18136): GeoCoder-doInBackGround 
04-18 08:36:42.972: W/dalvikvm(18136): threadid=25: thread exiting with uncaught exception (group=0x40015578) 
04-18 08:36:42.995: E/AndroidRuntime(18136): FATAL EXCEPTION: AsyncTask #15 
04-18 08:36:42.995: E/AndroidRuntime(18136): java.lang.RuntimeException: An error occured while executing doInBackground() 
04-18 08:36:42.995: E/AndroidRuntime(18136): at android.os.AsyncTask$3.done(AsyncTask.java:200) 
04-18 08:36:42.995: E/AndroidRuntime(18136): at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:274) 
04-18 08:36:42.995: E/AndroidRuntime(18136): at java.util.concurrent.FutureTask.setException(FutureTask.java:125) 
04-18 08:36:42.995: E/AndroidRuntime(18136): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:308) 
04-18 08:36:42.995: E/AndroidRuntime(18136): at java.util.concurrent.FutureTask.run(FutureTask.java:138) 
04-18 08:36:42.995: E/AndroidRuntime(18136): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088) 
04-18 08:36:42.995: E/AndroidRuntime(18136): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581) 
04-18 08:36:42.995: E/AndroidRuntime(18136): at java.lang.Thread.run(Thread.java:1019) 
04-18 08:36:42.995: E/AndroidRuntime(18136): Caused by: java.lang.NullPointerException 
04-18 08:36:42.995: E/AndroidRuntime(18136): at com.jetdelivery.mobile.statuspage$GeoCoder.doInBackground(statuspage.java:115) 
04-18 08:36:42.995: E/AndroidRuntime(18136): at com.jetdelivery.mobile.statuspage$GeoCoder.doInBackground(statuspage.java:1) 
04-18 08:36:42.995: E/AndroidRuntime(18136): at android.os.AsyncTask$2.call(AsyncTask.java:185) 
04-18 08:36:42.995: E/AndroidRuntime(18136): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:306) 
04-18 08:36:42.995: E/AndroidRuntime(18136): ... 4 more 

謝謝你們!

回答

3

從頁面Geocoder.getFromLocation

返回地址對象的列表。如果未找到匹配或沒有可用的後端服務,則返回null或空列表。

所以你需要前檢查null:

if (addresses.size() > 0) 
+0

如果它確實返回null,那麼他將不得不檢查地址!= null fisrt,否則他將崩潰 – 2012-04-17 21:09:29

+0

不是當然我明白髮生了什麼事。所以我必須檢查getFromLocation!= null? – 2012-04-17 21:11:43

+0

如果地址== null,則跳過引用它的代碼t的其餘部分。 – NickT 2012-04-17 21:18:57

-1

試試這個代碼

嘗試{

List<Address> addresses = gc.getFromLocation(latitude, 
      longitude, 1); 
    sb = new StringBuilder(); 
     if (addresses!=null && addresses.size() > 0) { 
      address = addresses.get(0); 
      int noOfMaxAddressLine = address.getMaxAddressLineIndex(); 
      if(noOfMaxAddressLine > 0){ 
       for (int i = 0; i < address.getMaxAddressLineIndex(); i++){ 
        sb.append(address.getAddressLine(i)).append("\n"); 
       } 
       addressString = sb.toString(); 
      } 
     } 
    } catch (Exception e) { 
     addressString = e.toString(); 
    } 
+0

對Saneesh的回覆很晚抱歉。我已經嘗試了您向我建議的代碼,但它仍然執行相同的操作。 =/ – 2012-04-18 15:23:40

+0

您可以給新的logcat輸出 – San 2012-04-18 15:25:44

+0

感謝您的快速回復!我真的很感激你的幫助。繼承人我的新日誌 – 2012-04-18 15:37:40

0

我得到一個(IOException異常e)就崩潰每次geocoder.getFromLocation(lat,lng,1); 不過,我只是打開/關閉我的三星手機,它的工作...