getIntent始終返回null,我無法弄清楚問題所在。我用putExtra()來存儲字符串,我在另一個活動中使用getIntent.getStringExtra()來調用它。Java getIntent.getStringExtra返回null
這與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);
和我使用這行代碼來檢查它是否爲空,其中它返回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();
}
好了,問題是這些值是雙打,所以現在我想弄清楚如何將double轉換爲字符串。這是我有:
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);
每個代碼是哪一類?你是否開始通過傳達意圖從「放置」的活動中「獲得」的活動? –