2013-10-24 64 views
0

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

每個代碼是哪一類?你是否開始通過傳達意圖從「放置」的活動中「獲得」的活動? –

回答

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

我剛剛意識到我的經緯度存儲爲雙打。你會碰巧知道一種將雙打轉換爲字符串值的方法嗎?謝謝 – user2905867

+0

@ user2905867使用'String.valueOf(double d)'和'Double.parseDouble(String s)' –

+0

好吧,我添加了這些,但它仍然返回null。我究竟做錯了什麼? '字符串latitude2 =將String.valueOf(緯度);'' 字符串longitude2 =將String.valueOf(緯度);'' 雙LATI = Double.parseDouble(latitude2.toString());'' 雙LNG =雙.parseDouble(longitude2.toString());' – user2905867