GoogleApiClient 问题:GoogleApiClient 尚未连接
GoogleApiClient issue: GoogleApiClient is not connected yet
在此class,:
public class Presence implements ConnectionCallbacks,
OnConnectionFailedListener, LocationListener
我有以下构造函数:
private Presence(Context context)
{
this.context = context;
gApiClient = new GoogleApiClient.Builder(context, this, this)
.addApi(LocationServices.API)
.build();
if (!gApiClient.isConnecting() && !gApiClient.isConnected())
{
gApiClient.connect();
}
} // of constructor()
我用它来 return 一个 Singleton
实例:
public static synchronized Presence getInstance(Context context)
{
if (presenceSingleton == null)
presenceSingleton = new Presence(context);
return presenceSingleton;
}
onConnected()
看起来像这样:
@Override
public void onConnected(Bundle connectionHint)
{
Log.e(LOG_TAG, "In onConnected(), gApiClient.isConnected(): " +
gApiClient.isConnected());
createLocationRequest();
getLocation();
getSubLocality();
} // of onConnected()
根据用户可以在应用程序中进行的设置,我调用以下方法将应用程序置于所谓的自动驾驶模式,它开始跟踪用户的位置:
public void startLocationUpdates()
{
// Prints 'false' in the logs:
Log.e(LOG_TAG, "In startLocationUpdates(), gApiClient.isConnected(): " + gApiClient.isConnected());
Intent locationChangeIntent = new Intent(context, LocationTracker.class);
pendingIntent = PendingIntent.getService(context, 188, locationChangeIntent, PendingIntent.FLAG_UPDATE_CURRENT);
// Crash points to the following line:
LocationServices.FusedLocationApi.requestLocationUpdates(gApiClient, locationRequest, pendingIntent);
} // of startLocationUpdates()
在主体 activity 中,我在 onCreate()
中创建了上述 class 的实例:
public class MainClass extends AppCompatActivity implements
OnSharedPreferenceChangeListener
{
....
....
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
getAppSettings();
presence = Presence.getInstance(getApplicationContext());
....
....
startApp();
....
}
private void startApp()
{
if (pref_autoPilot)
presence.startLocationUpdates();
}
....
....
....
} // of class MainClass
当用户设置自动驾驶首选项时,应用程序崩溃并出现以下异常:
java.lang.IllegalStateException: GoogleApiClient is not connected yet.
在上述方法中指示的行startLocationUpdates()
。
我看了很多答案,但无法找到解决这个问题的办法。你能指出我做错了什么吗? Presence
class 是否应该在 AppCompatActivity
或 FragmentActivity
或类似的位置,并且不能像这里那样独立?请帮我解决这个烦人的问题。
非常感谢!
遵循 Android Developers 中的简单指南。
我想你错过了以下部分:
protected void onStart() {
mGoogleApiClient.connect();
super.onStart();
}
protected void onStop() {
mGoogleApiClient.disconnect();
super.onStop();
}
在你的MainActivity
中你需要在需要时调用connect
和disconnect
(换句话说,当所谓的自动驾驶模式 开启)
You should instantiate a client object in your Activity's onCreate(Bundle) method and then call connect() in onStart() and disconnect() in onStop(), regardless of the state.
您应该从 onConnected()
方法中调用 presence.startLocationUpdates();
此方法。如果您的位置为空,请在调用前检查条件。在 GoogleApiClient 发生连接之前,您无法更新位置。
为了完整起见,@Kedi...
Can you please point out what I am doing wrong?
gApiClient.connect()
没有在正确的地方被调用。请查看已接受的答案。
Is it that the Presence
class should be in an AppCompatActivity
or FragmentActivity
or similar, and cannot be independent like in here?
它可以独立class
感谢大家的热心帮助。
在此class,:
public class Presence implements ConnectionCallbacks,
OnConnectionFailedListener, LocationListener
我有以下构造函数:
private Presence(Context context)
{
this.context = context;
gApiClient = new GoogleApiClient.Builder(context, this, this)
.addApi(LocationServices.API)
.build();
if (!gApiClient.isConnecting() && !gApiClient.isConnected())
{
gApiClient.connect();
}
} // of constructor()
我用它来 return 一个 Singleton
实例:
public static synchronized Presence getInstance(Context context)
{
if (presenceSingleton == null)
presenceSingleton = new Presence(context);
return presenceSingleton;
}
onConnected()
看起来像这样:
@Override
public void onConnected(Bundle connectionHint)
{
Log.e(LOG_TAG, "In onConnected(), gApiClient.isConnected(): " +
gApiClient.isConnected());
createLocationRequest();
getLocation();
getSubLocality();
} // of onConnected()
根据用户可以在应用程序中进行的设置,我调用以下方法将应用程序置于所谓的自动驾驶模式,它开始跟踪用户的位置:
public void startLocationUpdates()
{
// Prints 'false' in the logs:
Log.e(LOG_TAG, "In startLocationUpdates(), gApiClient.isConnected(): " + gApiClient.isConnected());
Intent locationChangeIntent = new Intent(context, LocationTracker.class);
pendingIntent = PendingIntent.getService(context, 188, locationChangeIntent, PendingIntent.FLAG_UPDATE_CURRENT);
// Crash points to the following line:
LocationServices.FusedLocationApi.requestLocationUpdates(gApiClient, locationRequest, pendingIntent);
} // of startLocationUpdates()
在主体 activity 中,我在 onCreate()
中创建了上述 class 的实例:
public class MainClass extends AppCompatActivity implements
OnSharedPreferenceChangeListener
{
....
....
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
getAppSettings();
presence = Presence.getInstance(getApplicationContext());
....
....
startApp();
....
}
private void startApp()
{
if (pref_autoPilot)
presence.startLocationUpdates();
}
....
....
....
} // of class MainClass
当用户设置自动驾驶首选项时,应用程序崩溃并出现以下异常:
java.lang.IllegalStateException: GoogleApiClient is not connected yet.
在上述方法中指示的行startLocationUpdates()
。
我看了很多答案,但无法找到解决这个问题的办法。你能指出我做错了什么吗? Presence
class 是否应该在 AppCompatActivity
或 FragmentActivity
或类似的位置,并且不能像这里那样独立?请帮我解决这个烦人的问题。
非常感谢!
遵循 Android Developers 中的简单指南。 我想你错过了以下部分:
protected void onStart() {
mGoogleApiClient.connect();
super.onStart();
}
protected void onStop() {
mGoogleApiClient.disconnect();
super.onStop();
}
在你的MainActivity
中你需要在需要时调用connect
和disconnect
(换句话说,当所谓的自动驾驶模式 开启)
You should instantiate a client object in your Activity's onCreate(Bundle) method and then call connect() in onStart() and disconnect() in onStop(), regardless of the state.
您应该从 onConnected()
方法中调用 presence.startLocationUpdates();
此方法。如果您的位置为空,请在调用前检查条件。在 GoogleApiClient 发生连接之前,您无法更新位置。
为了完整起见,@Kedi...
Can you please point out what I am doing wrong?
gApiClient.connect()
没有在正确的地方被调用。请查看已接受的答案。
Is it that the
Presence
class should be in anAppCompatActivity
orFragmentActivity
or similar, and cannot be independent like in here?
它可以独立class
感谢大家的热心帮助。