私はボタンを持っています、それをクリックしますogpsからユーザーの現在の場所を取得する必要があります。
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