Ham*_*jan 24 android google-api-client android-fragments google-places-api
还有其他方法可以连接Google API客户端吗?
我使用自动完成的地方,我必须在MYFRAGMENT的某些地方使用此代码
mGoogleApiClient = new GoogleApiClient.Builder(MainActivity.this)
.addApi(Places.GEO_DATA_API)
.enableAutoManage(this, GOOGLE_API_CLIENT_ID, this)
.addConnectionCallbacks(this).build();
Run Code Online (Sandbox Code Playgroud)
我的问题
enableAutoManage(this, GOOGLE_API_CLIENT_ID, this)
.addConnectionCallbacks(this).build();
Run Code Online (Sandbox Code Playgroud)
我不能处理它,因为当我替换我时this,getActivity()我有许多铸造问题
感谢您的帮助,如果这个问题很愚蠢,我很抱歉.
Mat*_*ape 61
如果你想使用enableAutoManage那么你必须扩展你的活动FragmentActivity.它所做的回调是自动管理GoogleApiClient工作所必需的.因此,最简单的解决方案是添加extends FragmentActivity到您的活动中.然后你的演员表不会失败并导致应用程序在运行时崩溃.
另一种解决方案是自己管理api客户端.您将从enableAutoManage构建器中删除该行,并确保您自己connect/ disconnect从客户端.最常见的地方是onStart()/ onStop().就像是...
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mGoogleApiClient = new GoogleApiClient.Builder(MainActivity.this)
.addApi(Places.GEO_DATA_API)
.addConnectionCallbacks(this).build();
}
@Override
protected void onStart() {
super.onStart();
mGoogleApiClient.connect();
}
@Override
protected void onStop() {
super.onStop();
mGoogleApiClient.disconnect();
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
22383 次 |
| 最近记录: |