无法通过 Intent 将值从 Child Activity(Google 地图)传递到 Parent activity
Unable to pass value through Intent from Child Activity (Google Maps) to Parent activity
我只想从 Google 地图 (children activity) 中检索纬度和经度值,使用 onActivityResult 将其传递回 parent activity。然而,检索到的值似乎总是 return 0。如有任何帮助,我们将不胜感激!
Parent Activity:
Intent intent;
intent = new Intent(TheInput.this, GoogleMap.class);
startActivityForResult(intent, 5);
break;
...
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(requestCode == 5){
if(resultCode == RESULT_OK) {
latitude = getIntent().getDoubleExtra("LATITUDE",0);
longitude = getIntent().getDoubleExtra("LONGITUDE",0);
Toast.makeText(getApplicationContext(), "From the received data, \nLat is " + latitude + ", Lon is " + longitude, Toast.LENGTH_SHORT).show();
//The toast value shows 0
}
}
super.onActivityResult(requestCode, resultCode, data);
}
Child Activity(Google 地图):
List<Address> locationList = null;
Address address = locationList.get(0);
LatLng latLng = new LatLng(address.getLatitude(), address.getLongitude());
lat = address.getLatitude();
lon = address.getLongitude();
Intent intent = new Intent();
intent.putExtra("LATITUDE",lat);
intent.putExtra("LONGITUDE",lon);
setResult(RESULT_OK, intent);
Toast.makeText(getApplicationContext(), "Lat is " + lat + ", Lon is " + lon, Toast.LENGTH_SHORT).show();
//The toast shows that they are values here
找出我的错误,这部分代码在 Parent Activity
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(requestCode == 5){
if(resultCode == RESULT_OK) {
latitude = getIntent().getDoubleExtra("LATITUDE",0);
longitude = getIntent().getDoubleExtra("LONGITUDE",0);
}
}
super.onActivityResult(requestCode, resultCode, data);
}
应该这样写。
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(requestCode == 5){
if(resultCode == RESULT_OK) {
latitude = data.getDoubleExtra("LATITUDE",0);
longitude = data.getDoubleExtra("LONGITUDE",0);
}
}
super.onActivityResult(requestCode, resultCode, data);
}
我只想从 Google 地图 (children activity) 中检索纬度和经度值,使用 onActivityResult 将其传递回 parent activity。然而,检索到的值似乎总是 return 0。如有任何帮助,我们将不胜感激!
Parent Activity:
Intent intent;
intent = new Intent(TheInput.this, GoogleMap.class);
startActivityForResult(intent, 5);
break;
...
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(requestCode == 5){
if(resultCode == RESULT_OK) {
latitude = getIntent().getDoubleExtra("LATITUDE",0);
longitude = getIntent().getDoubleExtra("LONGITUDE",0);
Toast.makeText(getApplicationContext(), "From the received data, \nLat is " + latitude + ", Lon is " + longitude, Toast.LENGTH_SHORT).show();
//The toast value shows 0
}
}
super.onActivityResult(requestCode, resultCode, data);
}
Child Activity(Google 地图):
List<Address> locationList = null;
Address address = locationList.get(0);
LatLng latLng = new LatLng(address.getLatitude(), address.getLongitude());
lat = address.getLatitude();
lon = address.getLongitude();
Intent intent = new Intent();
intent.putExtra("LATITUDE",lat);
intent.putExtra("LONGITUDE",lon);
setResult(RESULT_OK, intent);
Toast.makeText(getApplicationContext(), "Lat is " + lat + ", Lon is " + lon, Toast.LENGTH_SHORT).show();
//The toast shows that they are values here
找出我的错误,这部分代码在 Parent Activity
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(requestCode == 5){
if(resultCode == RESULT_OK) {
latitude = getIntent().getDoubleExtra("LATITUDE",0);
longitude = getIntent().getDoubleExtra("LONGITUDE",0);
}
}
super.onActivityResult(requestCode, resultCode, data);
}
应该这样写。
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(requestCode == 5){
if(resultCode == RESULT_OK) {
latitude = data.getDoubleExtra("LATITUDE",0);
longitude = data.getDoubleExtra("LONGITUDE",0);
}
}
super.onActivityResult(requestCode, resultCode, data);
}