- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我的 Android 应用程序功能是在人行走时通过 GPS 显示累积距离。距离值显示为 null9.55355.9346855.94574547 并按秒增加。即使我没有移动手机,我的位置变化也非常敏感。但主要问题是如何添加该值而不使其增加长度。我正在考虑将字符串 dist 转换为 Double1 和 double2 += valueof double1。然后将 double2 转换为字符串并显示我的 TextView。但我不知道如何在android 中转换。首先,我什至不确定我的想法是否可行。
编辑:当我运行时,我的输出变为 1.093507E7 。为什么会有E7?它确实显示在我的 TextView 上,但很快就崩溃了。
我的java代码
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
最佳答案
看看你最近的编辑,我认为你想得太多了。尝试这样的事情。请注意,我们如何仅存储一个 double
距离值,并在用户每次旅行时递增该值。
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;
double dist = 0;
String value;
float[] result;
private static final long MINIMUM_DISTANCE_CHANGE_FOR_UPDATES = 0; // in Meters
private static final long MINIMUM_TIME_BETWEEN_UPDATES = 0;
// methods omitted
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]);
dist += results[0];
DecimalFormat df = new DecimalFormat("#.##"); // adjust this as appropriate
distanceText.setText(df.format(dist));
Toast.makeText(MainActivity.this,
"Distance",
Toast.LENGTH_LONG).show();
lat1=loc2.getLatitude();
lon1=loc2.getLongitude();
}
}
// methods omitted
关于java - 显示累计距离 GPS。转换字符串/ double ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21436652/
我对以下需要使用 SQL 查询而不是 plsql 来解决的问题感到困惑。这个想法是建立一个累积列来计算之前的所有月份。输入表看起来像 Month 1 2 3 .. 24 我需要建立下表:
我正在寻找一个整洁的解决方案,最好使用 tidyverse 这个问题符合this answer ,但它确实有一个额外的扭曲。我的数据有一个整体分组变量“grp”。在每个这样的组中,我想根据“试验”定义
我正在尝试在 Spotfire 中创建一个运行余额列,该列应该如下图所示。本质上,我想逐行计算“金额”列的累积总计,并且我希望它随着日期的变化从 0 开始。 我尝试过几个 OVER 函数:Sum([A
我是一名优秀的程序员,十分优秀!