我想实现自动完成的地方谷歌API在android。为此,我需要创建一个googleapicclient对象。我使用了以下代码:
private GoogleApiClient mGoogleApiClient;
mGoogleApiClient = new GoogleApiClient.Builder(getActivity())
.addApi(Drive.API)
.addScope(Drive.SCOPE_FILE)
.enableAutoManage(getActivity(), 0 /* clientId */, this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.build();
但是当片段开始时,它会抛出以下异常,并导致应用程序崩溃。
. lang。IllegalStateException:已经管理一个googleapicclientid为0
注意:这在FRAGMENT中执行
下面是manifest文件的配置。
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="com.example.permission.MAPS_RECEIVE"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme"
android:name="com.example.bluehorsesoftkol.ekplatevendor.Utils.EkPlateVendorApplication">
<activity
android:name="com.example.bluehorsesoftkol.ekplatevendor.activity.registration.ActivitySplashScreen"
android:label="@string/app_name"
android:screenOrientation="landscape">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.example.bluehorsesoftkol.ekplatevendor.activity.registration.ActivityLogin"
android:screenOrientation="landscape">
</activity>
<activity
android:name=".activity.vendor.ActivityHome"
android:screenOrientation="landscape">
</activity>
<activity android:name="com.example.bluehorsesoftkol.ekplatevendor.activity.vendor.ActivityAddVendor"
android:screenOrientation="landscape">
</activity>
<activity
android:name=".activity.vendor.CustomGalleryActivity"
android:screenOrientation="landscape">
<intent-filter>
<action android:name="ekplatevendor.ACTION_PICK" />
<action android:name="ekplatevendor.ACTION_MULTIPLE_PICK" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<uses-feature
android:glEsVersion="0x00020000"
android:required="true"/>
<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="@string/TEST_API_KEY"/>
</application>
所以,请帮忙解决这个问题。
谢谢。
在你的代码中试试
@Override
public void onStart() {
super.onStart();
if (mGoogleApiClient != null)
mGoogleApiClient.connect();
}
@Override
public void onStop() {
super.onStop();
if (mGoogleApiClient != null && mGoogleApiClient.isConnected()) {
mGoogleApiClient.stopAutoManage((Activity) context);
mGoogleApiClient.disconnect();
}
}
注意:从playstore版本10开始,你不必使用
mGoogleApiClient.stopAutoManage((Activity) context);
这是我如何为我的片段:
片段代码:
private synchronized void buildGoogleApiClient() {
mGoogleApiClient = new GoogleApiClient.Builder(getActivity())
.enableAutoManage(getActivity(), this)
.addConnectionCallbacks(this)
.addApi(LocationServices.API)
.build();
createLocaitonRequest();
}
private void createLocaitonRequest() {
mLocationRequest = new LocationRequest();
mLocationRequest.setInterval(UPDATE_INTERVAL_IN_MILLISECONDS);
mLocationRequest.setFastestInterval(FASTEST_UPDATE_INTERVAL_IN_MILLISECONDS);
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
}
onStart:
@Override
public void onStart() {
super.onStart();
mGoogleApiClient.connect();
}
onDestroy:
@Override
public void onDestroy() {
super.onDestroy();
mGoogleApiClient.stopAutoManage(getActivity());
mGoogleApiClient.disconnect();
}
我运行buildGoogleApiClient()方法在onCreate()
然后我使用MapView在我的XML为我的片段如下:
<com.google.android.gms.maps.MapView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/map" />
然后在我的片段当onCreateView我做以下操作:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
mView = inflater.inflate(R.layout.fragment_maps, container, false);
setRetainInstance(true);
setupMapsFragment(mView);
mMapView = (MapView) mView.findViewById(R.id.map);
if (mMapView != null) {
mMapView.onCreate(null);
mMapView.onResume();
mMapView.getMapAsync(this);
}
return mView;
}
如果导航是用navigationdrawer制作的,我确实得到了你的问题,当我加载片段然后再次按下导航菜单项,然后它崩溃了,所以为了解决这个问题,我这样做:
if (id == R.id.nav_map) {
if(checkMapsFragment == true){
Toast.makeText(this,"map is already loaded",Toast.LENGTH_LONG).show();
}
if(checkMapsFragment == false){
fragment = new MapsFragment();
checkMapsFragment = true;
}