2017-03-27 62 views
2

我從另一個類調用checkLocationOn方法

// Change Address Click Action 
    tvProfileChangeAddress.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 
      if (Build.VERSION.SDK_INT >= 23) { 
       if ((checkSelfPermission(android.Manifest.permission.ACCESS_FINE_LOCATION) 
         == PackageManager.PERMISSION_GRANTED)) { 
        GetLocation.checkLocationOn(Profile.this); 
       }else{ 
        AskPermissions.AskLocationPermission(Profile.this); 
       } 
       } else { 
       GetLocation.checkLocationOn(Profile.this); 
      } 
      } 
     }); 

我已經初始化我在CheckLocation方法GoogleApiClient。

// Building Google API Client 
protected static synchronized void buildGoogleApiClient(Context context){ 
    mGoogleApiClient = new GoogleApiClient.Builder(context) 
      .addApi(LocationServices.API) 
      .addConnectionCallbacks((GoogleApiClient.ConnectionCallbacks) context) 
      .addOnConnectionFailedListener((GoogleApiClient.OnConnectionFailedListener) context) 
      .build(); 
    mGoogleApiClient.connect(); 
    createLocationRequest(); 
    return; 

} 

同時初始化GoogleApiClient我的應用力停止給錯誤

03-27 16:43:13.930 5118-5118 /? E/AndroidRuntime:致命例外:main 進程:com.demo.FetchLocation,PID:5118 java.lang.ClassCastException:com.demo.FetchLocation.Profile無法轉換爲com.google.android.gms.common.api。 GoogleApiClient $ ConnectionCallbacks at com.demo.FetchLocation.GetLocation.buildGoogleApiClient(GetLocation.java:75) at com.demo.FetchLocation.GetLocation.checkLocationOn(GetLocation.java:68) at com.demo.FetchLocation.Profile $ 2。 onClick(Profile.java:128) at android.view.View.performClick(View.java:5269) at android.view.View $ PerformClick.run(View.java:21556) at android.os.Handler。 handleCallback(Handler.java:815) at android.os.Handler.dispatchMessage(Handler.java:104) at android.os.Looper.loop(Looper.java:207) at android.app.ActivityThread.main(ActivityThread.java:5769) at java .lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:789) at com.android.internal.os.ZygoteInit.main(ZygoteInit。 java:679)

有沒有人有解決方案?

+0

完整堆棧跟蹤 –

+0

'Profile'類是一個Activity嗎? – fluffyBatman

+0

yess個人資料是一個活動 –

回答

1

有你Profile活動實現既GoogleApiClient.ConnectionCallbacksGoogleApiClient.OnConnectionFailedListener接口。

當你執行以下,

.addConnectionCallbacks((GoogleApiClient.ConnectionCallbacks) context) 
      .addOnConnectionFailedListener((GoogleApiClient.OnConnectionFailedListener) context) 

你實際上並沒有在你傳遞的上下文中,而你的情況,屬於個人活動實施的這些接口。

+0

謝謝!那工作...我忘了執行那些在配置文件活動 –

+0

很高興它的工作。你能否也將我的答案標記爲已接受? :) – fluffyBatman

0

按照official documentation,正確的方法調用:

if (mGoogleApiClient == null) { 
    mGoogleApiClient = new GoogleApiClient.Builder(this) 
     .addConnectionCallbacks(this) 
     .addOnConnectionFailedListener(this) 
     .addApi(LocationServices.API) 
     .build(); 
} 
0
protected static synchronized void buildGoogleApiClient(Context context){ 
mGoogleApiClient = new GoogleApiClient.Builder(context) 
     .addApi(LocationServices.API) 
     .addConnectionCallbacks(this) 
     .addOnConnectionFailedListener(this) 
     .build(); 
mGoogleApiClient.connect(); 
createLocationRequest(); 
return; 

} 

編寫這個程序並實現您在accnic中實現的接口的所有方法。

相關問題