6

当连接丢失时(当用户清除 RAM 时),我尝试重新连接到 LocationClient。

我尝试使用此代码:

private final GooglePlayServicesClient.ConnectionCallbacks mConnectionCallback = new GooglePlayServicesClient.ConnectionCallbacks() {

    @Override
    public void onDisconnected() {
        mLocationClient.removeLocationUpdates(mLocationListener);
        mLocationClient.disconnect();
        mLocationClient= null;

        mLocationClient= new LocationClient(mContext, mConnectionCallback, mConnectionFailedCallback);
        mLocationClient.connect(); // NULL POINTER EXCEPTION
    }

    @Override
    public void onConnected(Bundle bundle) {
          ...
    }
};

但是我在里面得到了 NullPointerException mLocaitonClient.connect()

10-15 08:33:26.478: E/AndroidRuntime(19572): FATAL EXCEPTION: main
10-15 08:33:26.478: E/AndroidRuntime(19572): java.lang.NullPointerException
10-15 08:33:26.478: E/AndroidRuntime(19572):    at com.google.android.gms.internal.bh.a(Unknown Source)
10-15 08:33:26.478: E/AndroidRuntime(19572):    at com.google.android.gms.internal.k.f(Unknown Source)
10-15 08:33:26.478: E/AndroidRuntime(19572):    at com.google.android.gms.internal.k$e.onServiceConnected(Unknown Source)
10-15 08:33:26.478: E/AndroidRuntime(19572):    at com.google.android.gms.internal.l.a(Unknown Source)
10-15 08:33:26.478: E/AndroidRuntime(19572):    at com.google.android.gms.internal.k.connect(Unknown Source)
10-15 08:33:26.478: E/AndroidRuntime(19572):    at com.google.android.gms.location.LocationClient.connect(Unknown Source)
10-15 08:33:26.478: E/AndroidRuntime(19572):    at com.myapp.MyLocationClient$1.onDisconnected(MyLocationClient.java:92)
10-15 08:33:26.478: E/AndroidRuntime(19572):    at com.google.android.gms.internal.k.A(Unknown Source)
10-15 08:33:26.478: E/AndroidRuntime(19572):    at com.google.android.gms.internal.k$e.onServiceDisconnected(Unknown Source)
10-15 08:33:26.478: E/AndroidRuntime(19572):    at com.google.android.gms.internal.l$a$a.onServiceDisconnected(Unknown Source)
10-15 08:33:26.478: E/AndroidRuntime(19572):    at android.app.LoadedApk$ServiceDispatcher.doDeath(LoadedApk.java:1102)
10-15 08:33:26.478: E/AndroidRuntime(19572):    at android.app.LoadedApk$ServiceDispatcher$RunConnection.run(LoadedApk.java:1116)
10-15 08:33:26.478: E/AndroidRuntime(19572):    at android.os.Handler.handleCallback(Handler.java:615)
10-15 08:33:26.478: E/AndroidRuntime(19572):    at android.os.Handler.dispatchMessage(Handler.java:92)
10-15 08:33:26.478: E/AndroidRuntime(19572):    at android.os.Looper.loop(Looper.java:137)
10-15 08:33:26.478: E/AndroidRuntime(19572):    at android.app.ActivityThread.main(ActivityThread.java:4898)
10-15 08:33:26.478: E/AndroidRuntime(19572):    at java.lang.reflect.Method.invokeNative(Native Method)
10-15 08:33:26.478: E/AndroidRuntime(19572):    at java.lang.reflect.Method.invoke(Method.java:511)
10-15 08:33:26.478: E/AndroidRuntime(19572):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1006)
10-15 08:33:26.478: E/AndroidRuntime(19572):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773)
10-15 08:33:26.478: E/AndroidRuntime(19572):    at dalvik.system.NativeStart.main(Native Method)

如何修复它并重新连接?

4

3 回答 3

4

一个更简单的解决方案是在OnDisconnect.

public void onDisconnect(){
     //do nothing to client
}

当需要使用客户端时,只需检查是否已连接

if(mLocationClient.isconnected()){
     mLocationClient.connect();
}

Google Play 服务似乎可以轻松地重新连接。

我已经成功地在 4.0.4 和 4.2.2 上使用了它。

于 2013-10-18T21:46:30.400 回答
3

我找到了解决方案!只需使用Handler.

@Override
public void onDisconnected() {

    new Handler().post(new Runnable() {
        @Override
        public void run() {
            mLocationClient.removeLocationUpdates(mLocationListener);
            mLocationClient.disconnect();
            mLocationClient = null;

            mLocationClient = new LocationClient(mContext, mConnectionCallback, mConnectionFailedCallback);
            mLocationClient.connect(); // NOW WORKING
        }
    }
}
于 2013-10-17T17:23:07.497 回答
0

在官方文档(http://developer.android.com/reference/com/google/android/gms/location/LocationClient.html)中,它写道:

public void disconnect ()

“关闭与 Google Play 服务的连接。调用此方法后,无法对该对象进行任何调用。”

所以你不能connect()在之后调用 a ,你必须像第一次那样重新创建LocationClient对象才能再次连接。

于 2013-10-15T07:19:32.737 回答