Reputation: 118
I am calling checkLocationOn method from another class as
// 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);
}
}
});
I have Initialized My GoogleApiClient in CheckLocation method.
// 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;
}
while Initializing GoogleApiClient my Application force stops giving error
03-27 16:43:13.930 5118-5118/? E/AndroidRuntime: FATAL EXCEPTION: main Process: com.demo.FetchLocation, PID: 5118 java.lang.ClassCastException: com.demo.FetchLocation.Profile cannot be cast to 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)
Does anyone have solution for this ??
Upvotes: 2
Views: 3661
Reputation: 785
protected static synchronized void buildGoogleApiClient(Context context){
mGoogleApiClient = new GoogleApiClient.Builder(context)
.addApi(LocationServices.API)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.build();
mGoogleApiClient.connect();
createLocationRequest();
return;
}
write this and implement all methods of the interfaces you are implementing in your acctivity.
Upvotes: 0
Reputation: 6704
Have your Profile
activity implemented both GoogleApiClient.ConnectionCallbacks
and GoogleApiClient.OnConnectionFailedListener
interfaces.
When you're executing following,
.addConnectionCallbacks((GoogleApiClient.ConnectionCallbacks) context)
.addOnConnectionFailedListener((GoogleApiClient.OnConnectionFailedListener) context)
you don't actually have implemented any of those Interfaces in your passed context, which in your case, belongs to Profile activity.
Upvotes: 1
Reputation: 148
According to the official documentation, the right method call is:
if (mGoogleApiClient == null) {
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();
}
Upvotes: 0