API レベル 17 を対象とするアプリの onCreate メソッドにこのコードを挿入しました。
LocationManager lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
// connect to the GPS location service
Location loc = lm.getLastKnownLocation(lm.NETWORK_PROVIDER);
Toast.makeText(MainActivity.this,
"Latitude -> "+Double.toString(loc.getLatitude()) +
" Longitude is -> "+Double.toString(loc.getLongitude()),
Toast.LENGTH_LONG).show();
しかし、それは常に私に力を与えます。インターネット、FineLocation のアクセス許可があります。
08-14 00:33:18.057: E/AndroidRuntime(330): FATAL EXCEPTION: main
08-14 00:33:18.057: E/AndroidRuntime(330): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.b/com.example.b.MainActivity}: java.lang.NullPointerException
08-14 00:33:18.057: E/AndroidRuntime(330): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
08-14 00:33:18.057: E/AndroidRuntime(330): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
08-14 00:33:18.057: E/AndroidRuntime(330): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
08-14 00:33:18.057: E/AndroidRuntime(330): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
08-14 00:33:18.057: E/AndroidRuntime(330): at android.os.Handler.dispatchMessage(Handler.java:99)
08-14 00:33:18.057: E/AndroidRuntime(330): at android.os.Looper.loop(Looper.java:123)
08-14 00:33:18.057: E/AndroidRuntime(330): at android.app.ActivityThread.main(ActivityThread.java:4627)
08-14 00:33:18.057: E/AndroidRuntime(330): at java.lang.reflect.Method.invokeNative(Native Method)
08-14 00:33:18.057: E/AndroidRuntime(330): at java.lang.reflect.Method.invoke(Method.java:521)
08-14 00:33:18.057: E/AndroidRuntime(330): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
08-14 00:33:18.057: E/AndroidRuntime(330): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
08-14 00:33:18.057: E/AndroidRuntime(330): at dalvik.system.NativeStart.main(Native Method)
08-14 00:33:18.057: E/AndroidRuntime(330): Caused by: java.lang.NullPointerException
08-14 00:33:18.057: E/AndroidRuntime(330): at com.example.b.MainActivity.onCreate(MainActivity.java:23)
08-14 00:33:18.057: E/AndroidRuntime(330): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
08-14 00:33:18.057: E/AndroidRuntime(330): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
08-14 00:33:18.057: E/AndroidRuntime(330): ... 11 more