访问 onLocationChanged 中的共享首选项会强制关闭应用程序

发布于 2024-10-09 12:51:51 字数 4090 浏览 0 评论 0原文

在我的应用程序中,我有一个服务可以获取设备的位置并将此信息传递到线程。我还需要将共享首选项值传递给线程。当我这样做时,应用程序会出现运行时错误并强制关闭。如果我不尝试获取该值,其他所有内容都会起作用并传递到线程。

我认为发生这种情况是因为 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) 

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) 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

谈场末日恋爱 2024-10-16 12:51:52

这是您的第一个问题,

loc = new TrackGPS();

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

但即便如此,这

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

也应该只被调用一次。每次获得更新时它都会调用您的 onLocationChanged 。你不需要一直调用它。

编辑:哦,我知道你在那里做什么,但没有必要。您可以在 onCreate 中请求位置更新。

另外,

onDestroy();

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

最后,您

stopSelf();

在调用 requestslocationupdates 的线程之前调用。当然,这会导致未定义的行为。您正在停止服务,而不取消注册侦听器。

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.

下壹個目標 2024-10-16 12:51:52

根据你从不这样做的说法,这是错误的吗?

最终处理程序 mHandler_AboutGeneric = new Handler();

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

final Handler mHandler_AboutGeneric = new Handler();

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文