我从另一个类调用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:致命异常:主要 进程:com.demo.FetchLocation,PID:5118 java.lang.ClassCastException:com.demo.FetchLocation.Profile无法强制转换为com.google.android.gms.common.api.GoogleApiClient $ ConnectionCallbacks 在com.demo.FetchLocation.GetLocation.buildGoogleApiClient(GetLocation.java:75) 在com.demo.FetchLocation.GetLocation.checkLocationOn(GetLocation.java:68) 在com.demo.FetchLocation.Profile $ 2.onClick(Profile.java:128) 在android.view.View.performClick(View.java:5269) 在android.view.View $ PerformClick.run(View.java:21556) 在android.os.Handler.handleCallback(Handler.java:815) 在android.os.Handler.dispatchMessage(Handler.java:104) 在android.os.Looper.loop(Looper.java:207) 在android.app.ActivityThread.main(ActivityThread.java:5769) at java.lang.reflect.Method.invoke(Native Method) 在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:789) 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:679)
有没有人能解决这个问题?
答案 0 :(得分:1)
让您的Profile
活动同时实施GoogleApiClient.ConnectionCallbacks
和GoogleApiClient.OnConnectionFailedListener
接口。
当您执行以下操作时,
.addConnectionCallbacks((GoogleApiClient.ConnectionCallbacks) context)
.addOnConnectionFailedListener((GoogleApiClient.OnConnectionFailedListener) context)
您实际上并未在传递的上下文中实现任何这些接口,在您的情况下,这些接口属于个人资料活动。
答案 1 :(得分:0)
根据official documentation,正确的方法调用是:
if (mGoogleApiClient == null) {
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();
}
答案 2 :(得分: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;
}
编写并实现您在acctivity中实现的接口的所有方法。