2

我在Google Play服務庫中遇到了一些問題。Android Studio上的Google API錯誤

我已經導入這些文件:

import com.google.android.gms.location.LocationServices; 
import com.google.android.gms.common.GooglePlayServicesUtil; 
import com.google.android.gms.common.api.GoogleApiClient; 

我得到的錯誤在整個項目的 「GoogleApiClient」: -

public class MainActivity extends FragmentActivity implements LocationListener, 
    GoogleApiClient.ConnectionCallbacks, 
    GoogleApiClient.OnConnectionFailedListener 

在這裏我得到的錯誤與locationClient/LocationServices/GoogleApiClient

locationClient = new GoogleApiClient.Builder(this) 
       .addApi(LocationServices.API) 

這裏是「locationClient」的錯誤

@Override 
    public void onStop() { 
    // If the client is connected 
    if (locationClient.isConnected()) { 
     stopPeriodicUpdates(); 
    } 

    // After disconnect() is called, the client is considered "dead". 
    locationClient.disconnect(); 

    super.onStop(); 
    } 

    /* 
    * Called when the Activity is restarted, even before it becomes visible. 
    */ 
    @Override 
    public void onStart() { 
    super.onStart(); 

    // Connect to the location services client 
    locationClient.connect(); 
    } 

在這裏,我得到錯誤 「onConnectionSuspended」

@Override 
    public void onConnectionSuspended(int i) { 
     Log.i(Application.APPTAG, "GoogleApiClient connection has been suspend"); 
    } 
+2

記住:有一個錯誤?添加完整的logcat – piotrek1543

回答

2

確保您已包括谷歌Play服務,在您的build.gradle的依賴,例如:

dependencies { 
    compile fileTree(dir: 'libs', include: ['*.jar']) 

    compile 'com.android.support:appcompat-v7:23.1.1' 
    compile 'com.google.android.gms:play-services:8.3.0' 
} 
+0

確保您擁有清單和sha1代碼中的所有權限。 –

+1

@MohamedSalad是的,也需要這些,但沒有設置這些不會導致編譯錯誤,它會導致運行時錯誤。 –