Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/206.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/visual-studio-2012/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java getIntent.getStringExtra返回null_Java_Android - Fatal编程技术网

Java getIntent.getStringExtra返回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) ==

getIntent一直返回null,我无法找出问题所在。我使用了putExtra()来存储字符串,我在另一个活动中使用getIntent.getStringExtra()调用它

这是带有putExtra()的活动

这是带有getIntent()的活动

我用这行代码检查它是否为null,其中它确实返回null:

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");