访问onLocationChanged中的共享偏好会强制关闭应用程序

时间:2010-12-29 07:20:56

标签: android service gps sharedpreferences android-context

在我的应用程序中,我有一个获取设备位置的服务,并将此信息传递给一个线程。我还需要将sharedpreferences值传递给线程。当我这样做时,应用程序会收到运行时错误并强制关闭。如果我不试图获得该值,其他所有工作都会传递给线程。

我认为这是因为onLocationChanged()没有上下文,任何人都可以帮忙吗?

服务代码:

 public class TrackGPS extends Service implements LocationListener{  
     LocationManager lm;   
     LocationListener loc;  
     public SharedPreferences sharedpreferences;      
     public static final String US = "usersettings";  
     public String prefPhoneNum = "prefPhoneNum";  
     double lat = 0, lon = 0, alt = 0, acc = 0;  
     long tim = 0;  

    public void onCreate() {
           super.onCreate();  

          /* Calling the value works fine here but can't pass to onLocationChanged() */ 
          // sharedpreferences = getSharedPreferences(US, MODE_WORLD_READABLE);
          // prefPhoneNum = sharedpreferences.getString("prefPhoneNum" , "");

           lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
           loc = new TrackGPS();
           enableGPS();   
           onDestroy();
           stopSelf();
}

public void enableGPS() {  
    new CountDownTimer(120000, 1000) {                  //120 seconds
         public void onTick(long millisUntilFinished) 
         {
              lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, loc);             
         }
         public void onFinish() 
         {                  
                lm.removeUpdates(loc);      
         }
    }.start();         
}

@Override
public void onLocationChanged(Location location) {
    lat = location.getLatitude();
    lon = location.getLongitude();
    alt = location.getAltitude();
    acc = location.getAccuracy();
    tim = location.getTime();

    sharedpreferences = getSharedPreferences(US, MODE_WORLD_READABLE);
    prefPhoneNum = sharedpreferences.getString("prefPhoneNum" , "");

    Thread cThread = new Thread(new SocketsClient(lat, lon, alt, acc, tim, prefPhoneNum));
    cThread.start();
}
@Override
public void onProviderDisabled(String provider) {   }
@Override
public void onProviderEnabled(String provider)  {   }
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {   }
@Override
public IBinder onBind(Intent intent) { return null; }
public void onDestroy() { super.onDestroy(); }
}

Logcat错误:

D/LocationManager(3912): requestLocationUpdates: provider = gps, listener = accel.working.TrackGPS@4628bce0  
D/GpsLocationProvider(96): setMinTime 0  
D/GpsLocationProvider(96): startNavigating  
D/GpsLocationProvider(96): TTFF: 3227  
D/AndroidRuntime(912): Shutting down VM  
W/dalvikvm(3912): threadid=1: thread exiting with uncaught exception (group=0x400259f8)  
E/AndroidRuntime(3912): FATAL EXCEPTION: main  
E/AndroidRuntime(3912): java.lang.NullPointerException  
E/AndroidRuntime(3912): at android.content.ContextWrapper.getSharedPreferences(ContextWrapper.java:146)  
E/AndroidRuntime(3912): at accel.working.TrackGPS.onLocationChanged(TrackGPS.java:63)  
E/AndroidRuntime(3912): at android.location.LocationManager$ListenerTransport._handleMessage(LocationManager.java:191)
E/AndroidRuntime(3912): at android.location.LocationManager$ListenerTransport.access$000(LocationManager.java:124)  
E/AndroidRuntime(3912): at android.location.LocationManager$ListenerTransport$1.handleMessage(LocationManager.java:140)  
E/AndroidRuntime(3912): at android.os.Handler.dispatchMessage(Handler.java:99)  
E/AndroidRuntime(3912): at android.os.Looper.loop(Looper.java:144)  
E/AndroidRuntime(3912): at android.app.ActivityThread.main(ActivityThread.java:4937)  
E/AndroidRuntime(3912): at java.lang.reflect.Method.invokeNative(Native Method)  
E/AndroidRuntime(3912): at java.lang.reflect.Method.invoke(Method.java:521)  
E/AndroidRuntime(3912): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)  
E/AndroidRuntime(3912): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)  
E/AndroidRuntime(3912): at dalvik.system.NativeStart.main(Native Method) 

2 个答案:

答案 0 :(得分:1)

这是你的第一个问题

loc = new TrackGPS();

永远不要创建活动或服务的实例或任何其他类new的类。在那里,甚至不值得进入更多的代码。

但即便如此,这个

lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, loc);

只应调用一次。它会在每次获取更新时调用onLocationChanged。你不需要继续打电话。

编辑:哦,我知道你在那里做什么,而且没有必要。您可以在onCreate中请求LocationUpdates。

另外,

onDestroy();

你永远不应该自己调用这个方法。这是由框架调用的。不要这样做

最后,你正在打电话

stopSelf();

在调用requestlocationupdates的线程之前

。当然会导致未定义的行为。您正在停止该服务,而不会取消注册该侦听器。

答案 1 :(得分:0)

根据你从未做过的这句话,这是错的吗?

final Handler mHandler_AboutGeneric = new Handler();