0

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
4

1 に答える 1

0

MainActivity の 23 行目は次のようになっていると思います。

Toast.makeText(MainActivity.this, "Latitude -> "+Double.toString(loc.getLatitude())

この行の後に null チェックを入れる

 Location loc = lm.getLastKnownLocation(lm.NETWORK_PROVIDER);

ネットワークプロバイダーからの最後の既知の場所がなく、loc が null である可能性があります。

これを試して:

     LocationManager lm = (LocationManager)
                        getSystemService(Context.LOCATION_SERVICE);

     // connect to the GPS location service
     Location loc = lm.getLastKnownLocation(lm.NETWORK_PROVIDER);

     //Try to get it from the GPS_PROVIDER
     if (loc == null)
     {
       loc = lm.getLastKnownLocation(lm.GPS_PROVIDER);
     }

     if (loc != null){
        Toast.makeText(MainActivity.this, "Latitude -> "+Double.toString(loc.getLatitude())
                            +" Longitude is -> "+Double.toString(loc.getLongitude()),   Toast.LENGTH_LONG).show();
     }else{
        //If it's still null there is no other way to get the last location. Display a message if you like.
        Toast.makeText(MainActivity.this, "No location found", Toast.LENGTH_LONG).show();
     }
于 2013-08-14T00:39:34.697 に答える