2013-08-25 115 views
0

問題:應用程序將從設備讀取經度和緯度。執行此功能的活動效果很好的第一時間......但是,當一個進入活動第二次應用程序崩潰,給下面的NullPointerExceptionNullPointerException經度和緯度上的位置

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(); 

有人可以請幫忙!

+0

'SMSActivity'的第85行發生了什麼? – thepoosh

+0

我評論說out ... longitude = location.getLongitude(); –

回答

0

該代碼可能工作,因爲你說它只在第二次返回到活動時崩潰。您應該在活動onResume()回調期間重新分配位置對象。

+0

我實際上在比android 2.3.3更高的另一個設備上測試它,它立即崩潰。 –

+0

重新指定爲?當用戶離開活動時,活動將調用finish()函數。 –

+0

您提到它在用戶第二次進入活動時崩潰。我假定你發起了第二個活動,使這個活動進入onPaused(),然後onResume()狀態。嘗試運行位置= lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);在onResume()? – wakaka