1

我在一个 android 应用程序中有一个服务,它实现 locationListener 以跟踪用户更改位置。

每次更改位置时,我都想将其发送到服务器。

因此,在 onLocationChanged 方法中,我正在获取纬度和经度。我想通过网络将它们发送到服务器。因此,我创建了一个新线程(因为主线程上不允许网络操作)。但是,我得到了这个线程的 java.lang.NullPointerException。

我应该怎么办?

这是我的代码......

    public class SService extends Service implements LocationListener{

public class LocalBinder extends Binder {
    SplashService getService() {
        return SplashService.this;
    }
}

@Override
public void onCreate() {
    super.onCreate();
    mNM = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);

    //Shows notification
    showNotification();
  }

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    Log.i("SService", "Received start id " + startId + ": " + intent);


     // Sets parameters and location providers
    setLocationParam();

    return START_STICKY;
}

@Override
public void onDestroy() {
    super.onDestroy();
    // Cancel the persistent notification.
    mNM.cancel(NOTIFICATION);
}

@Override
public IBinder onBind(Intent intent) {
    return mBinder;
}

// This is the object that receives interactions from clients.  See
// RemoteService for a more complete example.
private final IBinder mBinder = new LocalBinder();



@Override
public void onLocationChanged(Location location) {

    // Getting latitude of the current location
    latitude = location.getLatitude();

    // Getting longitude of the current location
    longitude = location.getLongitude();        

    Log.v("SSERVICE",latitude+"    "+longitude);

    final SMessage sm=new SMessage("0", latitude, longitude,"");

            // Creates new thread
    new SThread(sm).start();    

}

@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

}

}

4

0 回答 0