問題:アプリケーションは、デバイスから経度と緯度の読み取り値を取得することです。この機能を実行するアクティビティは、最初はうまく機能します...しかし、2回目にアクティビティに入ると、アプリケーションがクラッシュし、次のようになりますNullPointerException
:
08-25 19:53:36.517: E/AndroidRuntime(26672): FATAL EXCEPTION: main
08-25 19:53:36.517: E/AndroidRuntime(26672): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.vaw.selfhelp/com.vaw.selfhelp.SmsActivity}: java.lang.NullPointerException
08-25 19:53:36.517: E/AndroidRuntime(26672): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1651)
08-25 19:53:36.517: E/AndroidRuntime(26672): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
08-25 19:53:36.517: E/AndroidRuntime(26672): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
08-25 19:53:36.517: E/AndroidRuntime(26672): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
08-25 19:53:36.517: E/AndroidRuntime(26672): at android.os.Handler.dispatchMessage(Handler.java:99)
08-25 19:53:36.517: E/AndroidRuntime(26672): at android.os.Looper.loop(Looper.java:130)
08-25 19:53:36.517: E/AndroidRuntime(26672): at android.app.ActivityThread.main(ActivityThread.java:3687)
08-25 19:53:36.517: E/AndroidRuntime(26672): at java.lang.reflect.Method.invokeNative(Native Method)
08-25 19:53:36.517: E/AndroidRuntime(26672): at java.lang.reflect.Method.invoke(Method.java:507)
08-25 19:53:36.517: E/AndroidRuntime(26672): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
08-25 19:53:36.517: E/AndroidRuntime(26672): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
08-25 19:53:36.517: E/AndroidRuntime(26672): at dalvik.system.NativeStart.main(Native Method)
08-25 19:53:36.517: E/AndroidRuntime(26672): Caused by: java.lang.NullPointerException
08-25 19:53:36.517: E/AndroidRuntime(26672): at com.vaw.selfhelp.SmsActivity.onCreate(SmsActivity.java:85)
08-25 19:53:36.517: E/AndroidRuntime(26672): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
08-25 19:53:36.517: E/AndroidRuntime(26672): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615)
08-25 19:53:36.517: E/AndroidRuntime(26672): ... 11 more
コード:
lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
locationListener = new LocationListener() {
@Override
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub
latitude = location.getLatitude();
longitude = location.getLongitude();
}
@Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onStatusChanged(String provider, int status,
Bundle extras) {
// TODO Auto-generated method stub
}
};
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 2000, 10, locationListener);
longitude = location.getLongitude(); //this is line 85 which the logcat is referring to
latitude = location.getLatitude();
誰か助けてください!