Java getIntent.getStringExtra返回null
getIntent一直返回null,我无法找出问题所在。我使用了putExtra()来存储字符串,我在另一个活动中使用getIntent.getStringExtra()调用它 这是带有putExtra()的活动 这是带有getIntent()的活动 我用这行代码检查它是否为null,其中它确实返回null:Java getIntent.getStringExtra返回null,java,android,Java,Android,getIntent一直返回null,我无法找出问题所在。我使用了putExtra()来存储字符串,我在另一个活动中使用getIntent.getStringExtra()调用它 这是带有putExtra()的活动 这是带有getIntent()的活动 我用这行代码检查它是否为null,其中它确实返回null: if(getIntent().getStringExtra("lati") == null && (getIntent().getIntExtra("lng", 0) ==
if(getIntent().getStringExtra("lati") == null && (getIntent().getIntExtra("lng", 0) == 0))
{
invalid = true;
Toast.makeText(getApplicationContext(), "Latitude and longitude is null", Toast.LENGTH_SHORT).show();
}
好的,问题是这些值是双精度的,所以现在我想知道如何将双精度转换成字符串。这就是我所拥有的:
public void onLocationChanged(Location location) {
TextView tvLatitude = (TextView) findViewById(R.id.tv_latitude);
TextView tvLongitude = (TextView) findViewById(R.id.tv_longitude);
// Getting latitude of the current location
double latitude = location.getLatitude();
// Getting longitude of the current location
double longitude = location.getLongitude();
// Creating a LatLng object for the current location
LatLng latLng = new LatLng(latitude, longitude);
// Showing the current location in Google Map
googleMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
// Zoom in the Google Map
googleMap.animateCamera(CameraUpdateFactory.zoomTo(15));
// Setting latitude and longitude in the TextView tv_location
tvLatitude.setText("Latitude:" + location.getLatitude());
tvLongitude.setText("Longitude:" + location.getLongitude());
// Storing latitude and longitude to string
String lati = tvLatitude.getText().toString();
String lng = tvLongitude.getText().toString();
Intent i = new Intent(this, NoteActivity.class);
i.putExtra("lati", lati);
i.putExtra("lng", lng);
编辑:所以我将double存储为字符串,看起来不错,但仍然返回null。以下是新代码:
public void onLocationChanged(Location location) {
TextView tvLatitude = (TextView) findViewById(R.id.tv_latitude);
TextView tvLongitude = (TextView) findViewById(R.id.tv_longitude);
// Getting latitude of the current location
double latitude = location.getLatitude();
String latitude2 = String.valueOf(latitude);
// Getting longitude of the current location
double longitude = location.getLongitude();
String longitude2 = String.valueOf(longitude);
// Creating a LatLng object for the current location
LatLng latLng = new LatLng(latitude, longitude);
// Showing the current location in Google Map
googleMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
// Zoom in the Google Map
googleMap.animateCamera(CameraUpdateFactory.zoomTo(15));
// Setting latitude and longitude in the TextView tv_location
tvLatitude.setText("Latitude:" + latitude2);
tvLongitude.setText("Longitude:" + longitude2);
// Storing latitude and longitude to string
String lati = tvLatitude.getText().toString();
String lng = tvLongitude.getText().toString();
Intent i = new Intent(this, NoteActivity.class);
i.putExtra("lati", lati);
i.putExtra("lng", lng);
每个代码都来自哪个类?您是在通过传递意图而从活动中获取的活动吗?我刚刚意识到我的纬度和经度存储为双倍。您知道一种将双精度转换为字符串值的方法吗?Thanks@user2905867使用
String.valueOf(double d)
和double.parseDouble(String s)
可以,所以我添加了这些,但仍然返回null。我做错了什么<代码>字符串纬度2=String.valueOf(纬度)代码>String longitude2=String.valueOf(纬度)代码>double-lati=double.parseDouble(latitude2.toString())代码>double lng=double.parseDouble(longitude2.toString())代码>打印使用putExtra的值,这里的值为i.putExtra(“lati”,lati);i、 putExtra(“液化天然气”,lng)代码>
public void onLocationChanged(Location location) {
TextView tvLatitude = (TextView) findViewById(R.id.tv_latitude);
TextView tvLongitude = (TextView) findViewById(R.id.tv_longitude);
// Getting latitude of the current location
double latitude = location.getLatitude();
// Getting longitude of the current location
double longitude = location.getLongitude();
// Creating a LatLng object for the current location
LatLng latLng = new LatLng(latitude, longitude);
// Showing the current location in Google Map
googleMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
// Zoom in the Google Map
googleMap.animateCamera(CameraUpdateFactory.zoomTo(15));
// Setting latitude and longitude in the TextView tv_location
tvLatitude.setText("Latitude:" + location.getLatitude());
tvLongitude.setText("Longitude:" + location.getLongitude());
// Storing latitude and longitude to string
String lati = tvLatitude.getText().toString();
String lng = tvLongitude.getText().toString();
Intent i = new Intent(this, NoteActivity.class);
i.putExtra("lati", lati);
i.putExtra("lng", lng);
public void onLocationChanged(Location location) {
TextView tvLatitude = (TextView) findViewById(R.id.tv_latitude);
TextView tvLongitude = (TextView) findViewById(R.id.tv_longitude);
// Getting latitude of the current location
double latitude = location.getLatitude();
String latitude2 = String.valueOf(latitude);
// Getting longitude of the current location
double longitude = location.getLongitude();
String longitude2 = String.valueOf(longitude);
// Creating a LatLng object for the current location
LatLng latLng = new LatLng(latitude, longitude);
// Showing the current location in Google Map
googleMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
// Zoom in the Google Map
googleMap.animateCamera(CameraUpdateFactory.zoomTo(15));
// Setting latitude and longitude in the TextView tv_location
tvLatitude.setText("Latitude:" + latitude2);
tvLongitude.setText("Longitude:" + longitude2);
// Storing latitude and longitude to string
String lati = tvLatitude.getText().toString();
String lng = tvLongitude.getText().toString();
Intent i = new Intent(this, NoteActivity.class);
i.putExtra("lati", lati);
i.putExtra("lng", lng);
Intent intent = getIntent();
//If your extra data is represented as strings, then you can use this: intent.getStringExtra(String name)
//In your case:
String lati = intent.getStringExtra("lati");
String lng = intent.getStringExtra("lng");