我有一个按钮,我已将其设置为响应OnClick事件。因此,当单击按钮时,下面的代码将被执行。我希望按钮在单击时为用户的当前位置添加标记,但是当我单击按钮时会发生的情况是应用程序关闭。 OnClick按钮的名称为Add_Marker
。感谢
public void Add_Marker(View x)
{
gMap.setMyLocationEnabled(true);
LocationManager locationManager =
(LocationManager)getSystemService(LOCATION_SERVICE);
Criteria criteria = new Criteria();
String provider = locationManager.getBestProvider(criteria,true);
Location myLocation = locationManager.getLastKnownLocation(provider);
double latidude= myLocation.getLatitude();
double longitude = myLocation.getLongitude();
LatLng latlng= new LatLng(latidude,longitude);
gMap.moveCamera(CameraUpdateFactory.newLatLng(latlng));
gMap.animateCamera(CameraUpdateFactory.zoomTo(20));
gMap.addMarker(new MarkerOptions().position(new
LatLng(latidude,longitude)).title("You are here"));
}
logcat的
02-24 16:45:48.717: E/AndroidRuntime(17503): FATAL EXCEPTION: main
02-24 16:45:48.717: E/AndroidRuntime(17503): java.lang.IllegalStateException: Could not execute method of the activity
02-24 16:45:48.717: E/AndroidRuntime(17503): at android.view.View$1.onClick(View.java:3633)
02-24 16:45:48.717: E/AndroidRuntime(17503): at android.view.View.performClick(View.java:4240)
02-24 16:45:48.717: E/AndroidRuntime(17503): at android.view.View$PerformClick.run(View.java:17721)
02-24 16:45:48.717: E/AndroidRuntime(17503): at android.os.Handler.handleCallback(Handler.java:730)
02-24 16:45:48.717: E/AndroidRuntime(17503): at android.os.Handler.dispatchMessage(Handler.java:92)
02-24 16:45:48.717: E/AndroidRuntime(17503): at android.os.Looper.loop(Looper.java:137)
02-24 16:45:48.717: E/AndroidRuntime(17503): at android.app.ActivityThread.main(ActivityThread.java:5103)
02-24 16:45:48.717: E/AndroidRuntime(17503): at java.lang.reflect.Method.invokeNative(Native Method)
02-24 16:45:48.717: E/AndroidRuntime(17503): at java.lang.reflect.Method.invoke(Method.java:525)
02-24 16:45:48.717: E/AndroidRuntime(17503): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
02-24 16:45:48.717: E/AndroidRuntime(17503): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
02-24 16:45:48.717: E/AndroidRuntime(17503): at dalvik.system.NativeStart.main(Native Method)
02-24 16:45:48.717: E/AndroidRuntime(17503): Caused by: java.lang.reflect.InvocationTargetException
02-24 16:45:48.717: E/AndroidRuntime(17503): at java.lang.reflect.Method.invokeNative(Native Method)
02-24 16:45:48.717: E/AndroidRuntime(17503): at java.lang.reflect.Method.invoke(Method.java:525)
02-24 16:45:48.717: E/AndroidRuntime(17503): at android.view.View$1.onClick(View.java:3628)
02-24 16:45:48.717: E/AndroidRuntime(17503): ... 11 more
02-24 16:45:48.717: E/AndroidRuntime(17503): Caused by: java.lang.NullPointerException
02-24 16:45:48.717: E/AndroidRuntime(17503): at com.example.demomap.THEMAP.Add_Marker(THEMAP.java:109)
02-24 16:45:48.717: E/AndroidRuntime(17503): ... 14 more