2017-08-07 68 views
0

所以我跟着回答這個question變量不會被提交到電子郵件

我試圖把它融入我的代碼,但它不工作。

基本上我想獲得當前的座標,然後將它們提交給一個電子郵件地址。

這是一個答案,應該得到的座標和單擊該按鈕時,其寫入變量「LAT」和「結腸」

public LatLng getLocation() 
{ 
    // Get the location manager 
    LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE); 
    Criteria criteria = new Criteria(); 
    String bestProvider = locationManager.getBestProvider(criteria, false); 
    Location location = locationManager.getLastKnownLocation(bestProvider); 
    Double lat; 
    double lon; 
    try { 
     lat = location.getLatitude(); 
     lon = location.getLongitude(); 
     return new LatLng(lat, lon); 
    } 
    catch (NullPointerException e){ 
     e.printStackTrace(); 
     return null; 
    } 
} 

然後我嘗試發送電子郵件。這將打開電子郵件應用程序。但是應用程序應該自己編寫所有文本。 (我可以簡單地發送它在背景嗎?)在身體應該有一個小文本,然後我的座標,我在變量'lat'和'lon'中定義。可悲的是,它不承認我的變數。我需要改變什麼?

final Button button = (Button) findViewById(R.id.addbutton); 
     button.setOnClickListener(new View.OnClickListener() { 
      public void onClick(View v) { 
       getLocation(); 
       Intent i = new Intent(Intent.ACTION_SENDTO); 
       //i.setType("message/rfc822"); 
       i.putExtra(Intent.EXTRA_EMAIL , new String[]{"[email protected]"}); 
       i.putExtra(Intent.EXTRA_SUBJECT, "subject of email"); 
       i.putExtra(Intent.EXTRA_TEXT , lat + lon); 
       try { 
        startActivity(Intent.createChooser(i, "Send mail...")); 
       } catch (android.content.ActivityNotFoundException ex) { 
        Toast.makeText(MapsActivity.this, "There are no email clients installed.", Toast.LENGTH_SHORT).show(); 
       } 

      } 
     }); 

編輯

這是堆棧跟蹤錯誤:

08-07 09:59:58.339 31371-31371/com.example.testingmapingmarker23 E/AndroidRuntime: FATAL EXCEPTION: main 
                        Process: com.example.testingmapingmarker23, PID: 31371 
                        Theme: themes:{default=overlay:system, iconPack:system, fontPkg:system, com.android.systemui=overlay:system, com.android.systemui.navbar=overlay:system} 
                        java.lang.NullPointerException: Attempt to read from field 'double com.google.android.gms.maps.model.LatLng.latitude' on a null object reference 
                         at com.example.testingmapingmarker23.MapsActivity$1$1.onClick(MapsActivity.java:91) 
                         at android.view.View.performClick(View.java:5204) 
                         at android.view.View$PerformClick.run(View.java:21158) 
                         at android.os.Handler.handleCallback(Handler.java:739) 
                         at android.os.Handler.dispatchMessage(Handler.java:95) 
                         at android.os.Looper.loop(Looper.java:148) 
                         at android.app.ActivityThread.main(ActivityThread.java:5461) 
                         at java.lang.reflect.Method.invoke(Native Method) 
                         at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
                         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 

回答

0

你叫你的getLocation()方法的代碼,但你有沒有在任何地方使用的返回值。請按照以下步驟操作 -

final Button button = (Button) findViewById(R.id.addbutton); 
     button.setOnClickListener(new View.OnClickListener() { 
      public void onClick(View v) { 
       LatLng latlng = getLocation(); 
       Intent i = new Intent(Intent.ACTION_SENDTO); 
       //i.setType("message/rfc822"); 
       i.putExtra(Intent.EXTRA_EMAIL , new String[]{"[email protected]"}); 
       i.putExtra(Intent.EXTRA_SUBJECT, "subject of email"); 
       i.putExtra(Intent.EXTRA_TEXT , latlng.latitude+" " + latlng.longitude); 
       try { 
        startActivity(Intent.createChooser(i, "Send mail...")); 
       } catch (android.content.ActivityNotFoundException ex) { 
        Toast.makeText(MapsActivity.this, "There are no email clients installed.", Toast.LENGTH_SHORT).show(); 
       } 

      } 
     }); 

這裏我將返回值保存在latlng變量中,然後用它發送到文本。您還可以修改把多餘的線要做到這一點 -

i.putExtra(Intent.EXTRA_TEXT , latlng.toString()); 

這會給你的經緯度長格式 -

緯度/經度:(xx.xxxxxx,yy.yyyyy)

+0

它找不到'getLatitude'在i.putExtra行 – MrHarrison

+0

@MrHarrison更新了我的答案。由於它們是公共變量,因此可以直接訪問它們。只需交叉檢查類定義。 –

+0

謝謝。你是否也有一個在後臺發送郵件而不添加太多代碼的解決方案? – MrHarrison