所以我跟着回答了question:
我尝试将其集成到我的代码中,但它不起作用。
基本上我想获取当前坐标,然后将它们提交给电子邮件地址。
这是一个答案,应该得到坐标并将它们写入变量'lat'和'lon'
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[]{"mailadress@example.com"});
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();
}
}
});
修改
这是stacktrace中的错误:
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 :(得分: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[]{"mailadress@example.com"});
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变量中的返回值,然后使用它发送到文本。您还可以修改put额外行来执行此操作 -
i.putExtra(Intent.EXTRA_TEXT , latlng.toString());
这将以格式 -
为您提供lat longlat / lng :( xx.xxxxxx,yy.yyyyy)