为什么我的应用程序抛出IllegalStateException但GoogleApiClient尚未连接?

时间:2016-02-04 18:40:03

标签: java android google-api google-api-client

var posts = db.Posts
    .Include(x => x.Tags)
    .Include(x => x.Neighbourhood)
    .OrderByDescending(x => x.PostedDate);
if (id != null)
    posts = posts.Where(x => x.NeighbourhoodId == id.Value);
if (tagid != null)
    posts = posts.Where(x => x.Tags.Any(t => t.TagId == tagid.Value));
var ret = from data in posts
    // ... the rest

这来自我的服务(GetInformationService.onConnected)。如何说它在我的onConnected方法中没有连接是很奇怪的?我已经看过很多像这样的问题,但我仍然无法弄明白,因为所有的答案都说明了onCreate方法,但我没有onCreate,因为这是一项服务。这是我的相关代码(请记住我正在扩展IntentService):

java.lang.IllegalStateException: GoogleApiClient is not connected yet.

1 个答案:

答案 0 :(得分:2)

尝试在服务中加入onCreate()并从此处构建googApiClient

    @Override
public void onCreate() {
    super.onCreate();
    googleApiClient = new GoogleApiClient.Builder(this)
        .addApi(LocationServices.API)
        .addConnectionCallbacks(this)
        .addOnConnectionFailedListener(this)
        .build();
    googleApiClient.connect();
      }