我想通過我的距離和持續時間到下一個頁面,但結果顯示對於兩個0.0值
第一活動
btnStop.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v) {
startButtonClicked=false;
startDistance=false;
Double value=Double.valueOf(distance.getText().toString());
Double durationValue=time;
Intent intent = new Intent(MainActivity.this, FinishActivity.class);
intent.putExtra("dist", value);
intent.putExtra("time",durationValue);
startActivity(intent);
finish();
}
});
0.0
在它我印刷機後顯示
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_finish);
Bundle extras = getIntent().getExtras();
if (extras != null)
{
Double value = extras.getDouble("dist");
Double durationValue = extras.getDouble("time");
displayDistance=(TextView)findViewById(R.id.finishDistance);
displayDistance.setText("Distance: " + value);
displayDuration=(TextView)findViewById(R.id.finishDuration);
displayDuration.setText("Duration: " + durationValue);
}
的應用程序崩潰的活動停止
這裏是我的完整Java代碼的第一個活動(第二隻顯示它們)。如果你都不願看到我是如何讓他們
public class MainActivity extends FragmentActivity implements LocationListener{
protected LocationManager locationManager;
private GoogleMap googleMap;
Button btnStartMove,btnPause,btnResume,btnStop;
static double n=0;
Long s1,r1;
double dis=0.0;
Thread t1;
EditText userNumberInput;
boolean bool=false;
int count=0;
double speed = 1.6;
double lat1,lon1,lat2,lon2,lat3,lon3,lat4,lon4;
double dist = 0;
TextView distance;
Button btnDuration;
float[] result;
private static final long MINIMUM_DISTANCE_CHANGE_FOR_UPDATES =1; // in Meters
private static final long MINIMUM_TIME_BETWEEN_UPDATES = 4000; //in milliseconds
boolean startDistance = false;
boolean startButtonClicked = false;
MyCount counter;
int timer = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,MINIMUM_TIME_BETWEEN_UPDATES,MINIMUM_DISTANCE_CHANGE_FOR_UPDATES, this);
if(isGooglePlay())
{
setUpMapIfNeeded();
}
distance=(TextView)findViewById(R.id.Distance);
btnDuration=(Button)findViewById(R.id.Duration);
btnStartMove=(Button)findViewById(R.id.Start);//start moving
btnStop=(Button)findViewById(R.id.Stop);
//prepare distance...........
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);
btnStartMove.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v) {
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);
lat3 = location.getLatitude();
lon3 = location.getLongitude();
startButtonClicked=true;
counter= new MyCount(30000,1000);
counter.start();
Toast.makeText(MainActivity.this,
"start is true",
Toast.LENGTH_LONG).show();
}
});
btnStop.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v) {
startButtonClicked=false;
startDistance=false;
//String dis = (String) distance.getText();
//Double dd = Double.parseDouble(dis);
Intent intent = new Intent(MainActivity.this, FinishActivity.class);
//DecimalFormat df = new DecimalFormat("#.##");
//dist=(df.format(dd);
intent.putExtra("dist","value");
//intent.putExtra("speed","speedValue");
intent.putExtra("time","durationValue");
startActivity(intent);
finish();
}
});
btnDuration.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v) {
if(startButtonClicked=true)
{
double time=n*30+r1;
Toast.makeText(MainActivity.this,"Speed(m/s) :"+String.valueOf(dist/time)+"Duration :"+String.valueOf(time),Toast.LENGTH_LONG).show();
}
}
});
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()));
}
else if(location == null)
{
Toast.makeText(MainActivity.this,
"Location is null",
Toast.LENGTH_LONG).show();
}
}
private void setUpMapIfNeeded() {
if(googleMap == null)
{
Toast.makeText(MainActivity.this, "Getting map",
Toast.LENGTH_LONG).show();
googleMap =((SupportMapFragment)getSupportFragmentManager().findFragmentById(R.id.displayMap)).getMap();
if(googleMap != null)
{
setUpMap();
}
}a
}
private void setUpMap()
{
//Enable MyLocation Layer of Google Map
googleMap.setMyLocationEnabled(true);
//Get locationManager object from System Service LOCATION_SERVICE
//LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
//Create a criteria object to retrieve provider
Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE);
//Get the name of the best provider
String provider = locationManager.getBestProvider(criteria, true);
if(provider == null)
{
onProviderDisabled(provider);
}
//set map type
googleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
//Get current location
Location myLocation = locationManager.getLastKnownLocation(provider);
if(myLocation != null)
{
onLocationChanged(myLocation);
}
locationManager.requestLocationUpdates(provider, 0, 0, this);
}
private boolean isGooglePlay()
{
int status = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);
if (status == ConnectionResult.SUCCESS)
{
Toast.makeText(MainActivity.this, "Google Play Services is available",
Toast.LENGTH_LONG).show();
return(true);
}
else
{
GooglePlayServicesUtil.getErrorDialog(status, this, 10).show();
}
return (false);
}
@Override
public void onLocationChanged(Location myLocation) {
System.out.println("speed " + myLocation.getSpeed());
//show location on map.................
//Get latitude of the current location
double latitude = myLocation.getLatitude();
//Get longitude of the current location
double longitude = myLocation.getLongitude();
//Create a LatLng object for the current location
LatLng latLng = new LatLng(latitude, longitude);
//Show the current location in Google Map
googleMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
//Zoom in the Google Map
googleMap.animateCamera(CameraUpdateFactory.zoomTo(20));
googleMap.addMarker(new MarkerOptions().position(new LatLng(latitude, longitude)).title("You are here!"));
//show distance............................
if(startDistance == true)
{
Toast.makeText(MainActivity.this,
"Location has changed",
Toast.LENGTH_LONG).show();
if(myLocation != null)
{
//latitude.setText("Current Latitude: " + String.valueOf(loc2.getLatitude()));
//longitude.setText("Current Longitude: " + String.valueOf(loc2.getLongitude()));
float[] results = new float[1];
Location.distanceBetween(lat3, lon3, myLocation.getLatitude(), myLocation.getLongitude(), results);
System.out.println("Distance is: " + results[0]);
dist += results[0];
DecimalFormat df = new DecimalFormat("#.##"); // adjust this as appropriate
if(count==1)
{
distance.setText(df.format(dist) + "meters");
}
lat3=myLocation.getLatitude();
lon3=myLocation.getLongitude();
count=1;
}
}
if(startButtonClicked == true)
{
startDistance=true;
}
//}
}
@Override
public void onProviderDisabled(String provider) {
Toast.makeText(MainActivity.this,
"Provider disabled by the user. GPS turned off",
Toast.LENGTH_LONG).show();
}
@Override
public void onProviderEnabled(String provider) {
Toast.makeText(MainActivity.this,
"Provider enabled by the user. GPS turned on",
Toast.LENGTH_LONG).show();
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
Toast.makeText(MainActivity.this, "Provider status changed",
Toast.LENGTH_LONG).show();
}
@Override
protected void onPause() {
super.onPause();
locationManager.removeUpdates(this);
}
@Override
protected void onResume() {
super.onResume();
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,MINIMUM_TIME_BETWEEN_UPDATES,MINIMUM_DISTANCE_CHANGE_FOR_UPDATES, this);
}
public class MyCount extends CountDownTimer{
public MyCount(long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
}
@Override
public void onFinish() {
counter= new MyCount(30000,1000);
counter.start();
n=n+1;
}
@Override
public void onTick(long millisUntilFinished) {
s1=millisUntilFinished;
r1=(30000-s1)/1000;
//e1.setText(String.valueOf(r1));
}
}}
它說,當我拿走「」時,值不能解析爲變量。 value和durationValue都不會在我的代碼中啓動。在我遵循的例子中,我說必須把它放進去,但是可以識別dist和time。當我「」的價值和聯合國「」dist它有錯誤,方法putExtra(字符串,布爾)在類型意圖不適用於參數(雙,字符串) – Myst
我編輯了答案,所以看看如果現在有效,請立即投入並接受。 :D – SMR
如我的上面的代碼所示,我的dist和time都是代碼中生成的雙打,dist已經在textview中顯示,但只是在烤麪包的時候顯示。但在轉換之前,兩者都只是雙打。所以我按照最適合你的例子修改我的代碼,但是當我點擊停止按鈕時,它會崩潰。日誌貓消失,我不知道如何檢索。(我是新手)那麼第二頁呢?所以我getDouble(價值)/或(dist),因爲價值存儲在價值中。 – Myst