我得到了以下代码。我试图将我的坐标提交给电子邮件。
但每次按下按钮,应用程序都会关闭。
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();
Log.i("logging","lat "+lat+" long "+lon);
return new LatLng(lat, lon);
}
catch (NullPointerException e){
e.printStackTrace();
return null;
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
final Button button = (Button) findViewById(R.id.addbutton);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
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) {
makeText(MapsActivity.this, "There are no email clients installed.", LENGTH_SHORT).show();
}
}
});
}
});
如果按下按钮,将显示堆栈跟踪中的以下错误:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.testingmapingmarker23, PID: 6630
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:96)
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)
在对此对象执行任何操作之前,您应该检查latlng
。
e.g。
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");
if(latlng != null) {
i.putExtra(Intent.EXTRA_TEXT , latlng.latitude+" " + latlng.longitude);
} else {
i.putExtra(Intent.EXTRA_TEXT , "location not available");
}
try {
startActivity(Intent.createChooser(i, "Send mail..."));
} catch (android.content.ActivityNotFoundException ex) {
makeText(MapsActivity.this, "There are no email clients installed.", LENGTH_SHORT).show();
}
答案 1 :(得分:0)
如果位置存储在设备中,此代码仅为您提供LastKnownLocation。它不会始终为您提供最新的位置。如果您需要获取位置,则需要执行某些步骤。
android.location.LocationListener
(LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
requestLocationUpdates()
侦听器方法对当前位置的请求不可用。onLocationChanged()
方法并获取该方法中的当前位置。您可以按照以下提到的链接查看上述步骤的工作原理。
https://www.tutorialspoint.com/android/android_location_based_services.htm
http://javapapers.com/android/get-current-location-in-android/