我正在尝试将经度和纬度变量传递给 Google 地图片段的另一个活动。到目前为止,我已经设法做到了
Intent i = new Intent(Main.this, Main2.class);
i.putExtra(Double.toString(gettextLong), xLocation.getLongitude());
i.putExtra(Double.toString(gettextLat), xLocation.getLatitude());'
我如何在其他活动中收到它?
我正在尝试将经度和纬度变量传递给 Google 地图片段的另一个活动。到目前为止,我已经设法做到了
Intent i = new Intent(Main.this, Main2.class);
i.putExtra(Double.toString(gettextLong), xLocation.getLongitude());
i.putExtra(Double.toString(gettextLat), xLocation.getLatitude());'
我如何在其他活动中收到它?
尝试:
Intent i = new Intent(Main.this, Main2.class);
i.putExtra("lon", xLocation.getLongitude());
i.putExtra("lat", xLocation.getLatitude());
像这样:
int lat = getIntent().getExtras().getDouble("lat");
int lon = getIntent().getExtras().getDouble("lon");
给出相关的键,如
Intent i = new Intent(Main.this, Main2.class);
i.putExtra("longitude", xLocation.getLongitude());
i.putExtra("latitude", xLocation.getLatitude());
在 Main2 活动中
Bundle extras = getIntent().getExtras();
if(extras !=null && extras.getDouble("latitude") != null && extras.getDouble("longitude") != null) {
double lat = extras.getDouble("latitude");
double lng = extras.getDouble("longitude");
}
在您的第二个活动中使用getIntent()和getDoubleExtra() 。但是以不同的方式传递它。putExtra 中的第一个参数应该是一些 String 但你必须认识他,它需要稍后在第二个活动中使用。喜欢:
public static final String FIRST = "firstArgument";
i.putExtra(FIRST, xLocation.getLongitude());
在 Main2 中:
double i = getIntent().getDoubleExtra(Main.FIRST, 0);