我正在研究Google GeoFencing。 我在项目中使用tabView,创建Google API客户端时无法在上下文中使用“ this”关键字。 如果我调试手机,应用程序将崩溃。救救我
这是我的Java代码:
private Context mContext;// ive tried using like this,
mClient = new GoogleApiClient.Builder(getContext()) //ive also tried with getActivity();
.addConnectionCallbacks((GoogleApiClient.ConnectionCallbacks) getContext())
.addOnConnectionFailedListener((GoogleApiClient.OnConnectionFailedListener) getContext())
.addApi(LocationServices.API)
.addApi(Places.GEO_DATA_API)
.enableAutoManage((FragmentActivity) mContext,
(GoogleApiClient.OnConnectionFailedListener) getContext())
.build();
mClient.connect();
mGeofencing = new Geofencing(mClient,getContext());
答案 0 :(得分:1)
我认为您正在使用片段
请在getActivity()
中使用getContext()
private Context mContext;
mClient = new GoogleApiClient.Builder(getActivity())
.addConnectionCallbacks((GoogleApiClient.ConnectionCallbacks) getActivity())
.addOnConnectionFailedListener((GoogleApiClient.OnConnectionFailedListener) getActivity())
.addApi(LocationServices.API)
.addApi(Places.GEO_DATA_API)
.enableAutoManage((FragmentActivity) mContext, (GoogleApiClient.OnConnectionFailedListener) getActivity())
.build();
mClient.connect();
mGeofencing = new Geofencing(mClient,getActivity());
希望这对您有帮助...
谢谢
答案 1 :(得分:0)
使用getActivity()并为您的Fragment或活动实现GoogleApiClient.ConnectionCallbacks,GoogleApiClient.OnConnectionFailedListener。