0

getIntent 不断返回 null,我无法弄清楚问题所在。我使用了应该存储字符串的 putExtra(),我在另一个活动中使用 getIntent.getStringExtra() 调用它。

这是 putExtra() 的活动

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

这是 getIntent() 的活动

if (getIntent().getStringExtra("lati") != null && (getIntent().getStringExtra("lng") != null)){
String lati = bundle.getString("lati");
String lng = bundle.getString("lng");
addEntry(title, desc, lati, lng);
Intent i_note = new Intent(NoteActivity.this, GoogleMaps.class);
startActivity(i_note);

我正在使用这行代码来检查它是否为空,其中它确实返回空:

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

编辑:所以我将双精度数存储为字符串,它似乎很好,但它仍然返回 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);
4

1 回答 1

0
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");
于 2013-10-24T02:33:30.150 回答