2014-01-30 28 views
0

我的android應用程序的功能是通過gps顯示累計距離,當人走路時。 當我運行我的輸出變成1.093507E7。爲什麼有E7?它顯示在我的文本視圖上,但之後很快就會崩潰。gps累計距離轉換結果[0]加倍

 public class MainActivity extends Activity{ 

protected LocationManager locationManager; 
EditText userNumberInput; 
EditText userTextInput; 
TextView distanceText; 
TextView latitude; 
TextView longitude; 
double lat1,lon1,lat2,lon2,lat3,lon3,lat4,lon4; 
String dist = ""; 
String value; 
double finalDist1, finalDist2; 
float[] result; 
private static final long MINIMUM_DISTANCE_CHANGE_FOR_UPDATES = 0; // in Meters 
    private static final long MINIMUM_TIME_BETWEEN_UPDATES = 0; 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    distanceText=(TextView)findViewById(R.id.Distance); 
    latitude=(TextView)findViewById(R.id.currentLat); 
    longitude=(TextView)findViewById(R.id.currentLon); 

    locationManager = (LocationManager) getSystemService(LOCATION_SERVICE); 
    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,MINIMUM_TIME_BETWEEN_UPDATES,MINIMUM_DISTANCE_CHANGE_FOR_UPDATES, myLocationListener); 
    Log.d("GPS Enabled", "GPS Enabled"); 
    Criteria criteria = new Criteria(); 
    criteria.setAccuracy(Criteria.ACCURACY_FINE); 
    String provider = locationManager.getBestProvider(criteria, true); 
    Location location=locationManager.getLastKnownLocation(provider); 

    if(location!= null) 
    { 
     //Display current location in Toast 
     String message = String.format(
       "Current Location \n Longitude: %1$s \n Latitude: %2$s", 
       location.getLongitude(), location.getLatitude() 
     ); 
     Toast.makeText(MainActivity.this, message, 
       Toast.LENGTH_LONG).show(); 

     //Display current location in textview 
     latitude.setText("Current Latitude: " + String.valueOf(location.getLatitude())); 
     longitude.setText("Current Longitude: " + String.valueOf(location.getLongitude())); 
     lat1 = location.getLatitude(); 
     lon1 = location.getLongitude(); 
    } 
    else if(location == null) 
    { 
     Toast.makeText(MainActivity.this, 
       "Location is null",  
       Toast.LENGTH_LONG).show(); 
    } 

} 

private CharSequence ToString(double latitude2) { 
    // TODO Auto-generated method stub 
    return null; 
} 

LocationListener myLocationListener = new LocationListener() 
{ 
    public void onLocationChanged(Location loc2) 
    { 
     Toast.makeText(MainActivity.this, 
       "Location has changed",  
       Toast.LENGTH_LONG).show(); 
     if(loc2 != null) 
     { 
      latitude.setText("Current Latitude: " + String.valueOf(loc2.getLatitude())); 
      longitude.setText("Current Longitude: " + String.valueOf(loc2.getLongitude())); 
      float[] results = new float[1]; 
      Location.distanceBetween(lat1, lon1, loc2.getLatitude(), loc2.getLongitude(), results); 
      System.out.println("Distance is: " + results[0]);    


      if(dist!=null && String.valueOf(results[0])!=null) 
      { 
       dist += String.valueOf(results[0]); // your String 
       finalDist1 = Double.parseDouble(dist); 
       finalDist2 += finalDist1; 
       distanceText.setText(String.valueOf(finalDist2)); 
       Toast.makeText(MainActivity.this, 
         "Distance is accumulated",  
         Toast.LENGTH_LONG).show(); 
       lat1=loc2.getLatitude(); 
       lon1=loc2.getLongitude(); 
      } 


      Toast.makeText(MainActivity.this, 
        "Distance",  
        Toast.LENGTH_LONG).show(); 
      lat1=loc2.getLatitude(); 
      lon1=loc2.getLongitude(); 
     }   
} 

public void onProviderDisabled(String provider) 
{ 
    Toast.makeText(MainActivity.this, 
      "GPS is disabled", 
      Toast.LENGTH_LONG).show(); 
} 
public void onProviderEnabled(String provider) 
{ 
    Toast.makeText(MainActivity.this, 
      "GPS is enabled", 
      Toast.LENGTH_LONG).show(); 
} 
public void onStatusChanged(String provider, int status, 
     Bundle extras) 
{ 
    Toast.makeText(MainActivity.this, 
      "GPS status changed", 
      Toast.LENGTH_LONG).show(); 
} 

}; 
@Override 
protected void onPause() { 
super.onPause(); 
locationManager.removeUpdates(myLocationListener); 
} 
@Override 
protected void onResume() { 
    super.onResume(); 
    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,MINIMUM_TIME_BETWEEN_UPDATES,MINIMUM_DISTANCE_CHANGE_FOR_UPDATES, myLocationListener); 
}} 

日誌貓

01-29 23:53:04.814: D/gralloc_goldfish(1076): Emulator without GPU emulation detected. 
01-29 23:53:16.444: I/System.out(1076): Distance is: 1.093507E7 
01-29 23:53:16.614: I/Choreographer(1076): Skipped 45 frames! The application may be doing too much work on its main thread. 
01-29 23:53:23.784: I/Choreographer(1076): Skipped 57 frames! The application may be doing too much work on its main thread. 
01-29 23:53:34.774: I/System.out(1076): Distance is: 6412915.0 
01-29 23:53:34.774: D/AndroidRuntime(1076): Shutting down VM 
01-29 23:53:34.774: W/dalvikvm(1076): threadid=1: thread exiting with uncaught exception (group=0xb3a3dba8) 
01-29 23:53:34.814: E/AndroidRuntime(1076): FATAL EXCEPTION: main 
01-29 23:53:34.814: E/AndroidRuntime(1076): Process: com.example.validationapp, PID: 1076 
01-29 23:53:34.814: E/AndroidRuntime(1076): java.lang.NumberFormatException: Invalid double: "1.093507E76412915.0" 
01-29 23:53:34.814: E/AndroidRuntime(1076):  at java.lang.StringToReal.invalidReal(StringToReal.java:63) 
01-29 23:53:34.814: E/AndroidRuntime(1076):  at java.lang.StringToReal.initialParse(StringToReal.java:114) 
01-29 23:53:34.814: E/AndroidRuntime(1076):  at java.lang.StringToReal.parseDouble(StringToReal.java:263) 
01-29 23:53:34.814: E/AndroidRuntime(1076):  at java.lang.Double.parseDouble(Double.java:295) 
01-29 23:53:34.814: E/AndroidRuntime(1076):  at com.example.validationapp.MainActivity$1.onLocationChanged(MainActivity.java:112) 
01-29 23:53:34.814: E/AndroidRuntime(1076):  at android.location.LocationManager$ListenerTransport._handleMessage(LocationManager.java:279) 
01-29 23:53:34.814: E/AndroidRuntime(1076):  at android.location.LocationManager$ListenerTransport.access$000(LocationManager.java:208) 
01-29 23:53:34.814: E/AndroidRuntime(1076):  at android.location.LocationManager$ListenerTransport$1.handleMessage(LocationManager.java:224) 
01-29 23:53:34.814: E/AndroidRuntime(1076):  at android.os.Handler.dispatchMessage(Handler.java:102) 
01-29 23:53:34.814: E/AndroidRuntime(1076):  at android.os.Looper.loop(Looper.java:136) 
01-29 23:53:34.814: E/AndroidRuntime(1076):  at android.app.ActivityThread.main(ActivityThread.java:5017) 
01-29 23:53:34.814: E/AndroidRuntime(1076):  at java.lang.reflect.Method.invokeNative(Native Method) 
01-29 23:53:34.814: E/AndroidRuntime(1076):  at java.lang.reflect.Method.invoke(Method.java:515) 
01-29 23:53:34.814: E/AndroidRuntime(1076):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779) 
01-29 23:53:34.814: E/AndroidRuntime(1076):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595) 
01-29 23:53:34.814: E/AndroidRuntime(1076):  at dalvik.system.NativeStart.main(Native Method) 
01-29 23:53:38.814: I/Process(1076): Sending signal. PID: 1076 SIG: 9 
+0

E7是科學記數法中的指數,它代表了10的7次冪。 – NickT

+0

因此,如何讓它顯示在只有數字?你知道爲什麼我的應用程序崩潰嗎?我相信它是1.093507E76412915.0它將1.093507E7添加到6412915.0。如何增加值而不是增加長度? – Myst

+0

考慮爲我的答案upvote,它是正確的,並且可以避免科學輸出的錯誤。 – AlexWien

回答

1

可以避開E7,這是大flotaing點數科學記數法,由浮點數轉換爲整數值:

長distanceMeters =距離。
//或圓形(距離);

但是1E7意味着10百萬,所以在您的應用程序中有一個計算錯誤。 (0,0)