开发者

Accessing sharedpreferences within onLocationChanged forces close of application

In my app, I have a service that gets the location of the device and passes this info through to a thread. I also need to pass a sharedpreferences value through to the thread. When I do this, the application gets a runtime error and forces close开发者_如何学运维. Everything else works and is passed through to the thread if I don't try to get that value.

I presume this occurs because onLocationChanged() does not have a context, can anyone help with this please?

Service Code:

 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 Errors:

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) 


Well here's your first problem

loc = new TrackGPS();

Don't ever, EVER, create an instance of an activity or a service or any other class like that with new. It's not even worth going into more of your code with that there.

But even so, this

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

Should only be called once. It calls your onLocationChanged EVERY TIME it gets an update. You don't need to keep calling it.

EDIT: Oh I know what you're doing there, and it's not necessary. You can requestLocationUpdates in onCreate.

Also,

onDestroy();

You should never call this method yourself. This is called by the framework. Don't do it

And finally, you're calling

stopSelf();

before your thread that requestslocationupdates is ever called. Of course it will result in undefined behaviour. You're stopping the service, without unregistering the listener.


According to your never-ever-do-this-statement, is this wrong?

final Handler mHandler_AboutGeneric = new Handler();

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜