2016-08-02 10 views
1

私はこれで1週間以上立ち往生しており、私が間違っていることを正確に理解できないようです。私は私のために働くように見えるいずれも、次の質問に、読んだことがある:私は何をしようとしているGoogle APIクライアントに接続できません。

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

GoogleApiClient is throwing "GoogleApiClient is not connected yet" AFTER onConnected function getting called

google api client callback is never called

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

が使用されLocationServices APIをGeofenceクラスと組み合わせて使用​​して、ユーザーが指定された領域にいるかどうかを確認します。この問題は、Google APIクライアントおよび/またはLocationservices APIとの通信にあるように見えます。

私は私のマニフェストに次のように宣言した:

<service android:name=".GeofenceTransitionsIntentService" /> 

    <meta-data 
     android:name="com.google.android.gms.version" 
     android:value="@integer/google_play_services_version" /> 

    <meta-data 
     android:name="com.google.android.geo.API_KEY" 
     android:value="value of my key" /> 

</application> 

私はGoogleApiClientと対話する私のプロジェクトで宣言された2つのJavaメソッドがあります:

protected synchronized void buildGoogleApiClient() { 
    mGoogleApiClient = new GoogleApiClient.Builder(this) 
      .addConnectionCallbacks(this) 
      .addOnConnectionFailedListener(this) 
      .addApi(LocationServices.API) 
      .build(); 
} 

public void startApiClient(){ 

    if (mGoogleApiClient.isConnected()) { 
     Toast.makeText(this, getString(R.string.not_connected), Toast.LENGTH_SHORT).show(); 
     return; 

     //Of course the Toast is supposed to fire of when the user cannot 
     //connect to the google api. However when I make this code to run 
     //when the user cannot connect to google play services it just shows 
     //the toast and does not report me the error which is why I in this 
     //case made this code to run when the user is connected to google play services 

    } 

    try { 
     LocationServices.GeofencingApi.addGeofences 
       (mGoogleApiClient, getGeofencingRequest(), getGeofencePendingIntent()).setResultCallback(this); // Result processed in onResult(). This is also the line where the app seems to crash because it is unable to connect to the google api client 
    } catch (SecurityException securityException) { 
     securityException.notify(); 
    } 
} 

を私はその後、buildGoogleApiClient(コール)で私のonStart()メソッドでonCreate()メソッドとstartApiClient()を呼び出します。

@Override 
protected void onCreate(Bundle savedInstanceState) { 
super.onCreate(savedInstanceState); 
setContentView(R.layout.activity_navigation_drawer); 

… 

buildGoogleApiClient(); 
} 

_

@Override 
protected void onStart() { 
    super.onStart(); 

    mGoogleApiClient.connect(); 

    startApiClient(); 

} 

とケースneccessaryで、はonResult()メソッド:

public void onResult(Status status) { 
    if (status.isSuccess()) { 
     // Update state and save in shared preferences. 
     mGeofencesAdded = !mGeofencesAdded; 
     SharedPreferences.Editor editor = mSharedPreferences.edit(); 
     editor.putBoolean(Constants.GEOFENCES_ADDED_KEY, mGeofencesAdded); 
     editor.apply(); 
    } 

は、このことについて奇妙なことは、接続が実際にいくつかの時間のためにインスタンスを取得んということです。ユーザーが接続されていないときにトーストを表示するようにプログラムに指示すると、Androidモニターは接続が確立されたが、トーストは引き続きトリガーされると通知します。しかし、ユーザーがGoogle APIクライアントに接続しているときにトーストを表示するようにプログラムに指示すると、アプリがクラッシュし、Google APIクライアントがまだ接続されていないことがわかります。

お手数ではございますが、お手数をおかけしますようお願い申し上げます。私が非常に明白な何かを監督しているなら、私を許してください。

答えて

0

AndroidHiveのGoogleクライアントの場所 - Android Location API using Google Play Servicesのこのチュートリアルを試してみてください。

このチュートリアルで説明したように、ユーザーの現在地を取得するために、アクティビティの変更を行う必要があります。グーグルの利用について

  1. まずチェックプレイサービスがbuildGoogleApiClient()メソッドを呼び出すことでGoogleApiClientを構築し、デバイス上で利用可能になったらonResume()

  2. checkPlayServices()を呼び出してサービスを再生します。

  3. onStart()メソッドでmGoogleApiClient.connect()を呼び出してgoogle apiクライアントに接続します。これを呼び出すと、接続状況によってonConnectionFailed()onConnected()およびonConnectionSuspended()がトリガーされます。

  4. google apiが正常に接続されたら、displayLocation()onConnected()メソッドで呼び出して現在の場所を取得する必要があります。

また、あなたはまた、Troubleshoot the Geofence Entrance Eventで与えられるよう期待通りに動作していない警告の以下の考えられる理由を確認することができます。これらは次のとおりです。

  • ジオフェンス内で正確な位置が使用できないか、ジオフェンスが小さすぎます。
  • デバイスのWi-Fiがオフになっています。お使いの端末でWi-Fiとロケーションが有効になっていることを確認します。
  • ジオフェンス内に信頼性の高いネットワーク接続がありません。
  • 警告が遅くなる可能性があります。

重要な情報とサンプルコードはCreating and Monitoring Geofencesと指定のチュートリアルにあります。

関連する問題