我有一个按钮,单击它需要从 gps 获取用户当前位置。
我可以在没有 LocationListener 的情况下做到吗?因为我只需要在点击操作后才能获得位置,而且我不需要一直通过 onLocationChanged 吃电池。可能吗?
我试过了,但有错误
public void goToHomePoint(View v){
LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
Toast.makeText(getApplicationContext(), String.valueOf(location.getLatitude()) + " " + String.valueOf(location.getLongitude()), Toast.LENGTH_SHORT).show();
}
在清单中
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
错误
04-29 12:01:39.696: E/AndroidRuntime(252): Uncaught handler: thread main exiting due to uncaught exception
04-29 12:01:39.716: E/AndroidRuntime(252): java.lang.IllegalStateException: Could not execute method of the activity
04-29 12:01:39.716: E/AndroidRuntime(252): at android.view.View$1.onClick(View.java:2031)
04-29 12:01:39.716: E/AndroidRuntime(252): at android.view.View.performClick(View.java:2364)
04-29 12:01:39.716: E/AndroidRuntime(252): at android.view.View.onTouchEvent(View.java:4179)
04-29 12:01:39.716: E/AndroidRuntime(252): at android.view.View.dispatchTouchEvent(View.java:3709)
04-29 12:01:39.716: E/AndroidRuntime(252): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
04-29 12:01:39.716: E/AndroidRuntime(252): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
04-29 12:01:39.716: E/AndroidRuntime(252): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
04-29 12:01:39.716: E/AndroidRuntime(252): at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1659)
04-29 12:01:39.716: E/AndroidRuntime(252): at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1107)
04-29 12:01:39.716: E/AndroidRuntime(252): at android.app.Activity.dispatchTouchEvent(Activity.java:2061)
04-29 12:01:39.716: E/AndroidRuntime(252): at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1643)
04-29 12:01:39.716: E/AndroidRuntime(252): at android.view.ViewRoot.handleMessage(ViewRoot.java:1691)
04-29 12:01:39.716: E/AndroidRuntime(252): at android.os.Handler.dispatchMessage(Handler.java:99)
04-29 12:01:39.716: E/AndroidRuntime(252): at android.os.Looper.loop(Looper.java:123)
04-29 12:01:39.716: E/AndroidRuntime(252): at android.app.ActivityThread.main(ActivityThread.java:4363)
04-29 12:01:39.716: E/AndroidRuntime(252): at java.lang.reflect.Method.invokeNative(Native Method)
04-29 12:01:39.716: E/AndroidRuntime(252): at java.lang.reflect.Method.invoke(Method.java:521)
04-29 12:01:39.716: E/AndroidRuntime(252): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
04-29 12:01:39.716: E/AndroidRuntime(252): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
04-29 12:01:39.716: E/AndroidRuntime(252): at dalvik.system.NativeStart.main(Native Method)
04-29 12:01:39.716: E/AndroidRuntime(252): Caused by: java.lang.reflect.InvocationTargetException
04-29 12:01:39.716: E/AndroidRuntime(252): at com.artox.relax.inf.MapViewActivity.goToHomePoint(MapViewActivity.java:141)
04-29 12:01:39.716: E/AndroidRuntime(252): at java.lang.reflect.Method.invokeNative(Native Method)
04-29 12:01:39.716: E/AndroidRuntime(252): at java.lang.reflect.Method.invoke(Method.java:521)
04-29 12:01:39.716: E/AndroidRuntime(252): at android.view.View$1.onClick(View.java:2026)
04-29 12:01:39.716: E/AndroidRuntime(252): ... 19 more
04-29 12:01:39.716: E/AndroidRuntime(252): Caused by: java.lang.NullPointerException