Reputation: 21
I want to implement autocomplete place google API in android. To do that I need to create an object of GoogleApiClient. I have used the following code for this...
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();
But when the fragment starts, it throws the following exception and the app is crashed.
java.lang.IllegalStateException: Already managing a GoogleApiClient with id 0
Note: this is performed in FRAGMENT.
Following is the manifest file configuration..
<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>
So, please help to solve this issue..
Thank you.
Upvotes: 2
Views: 6188
Reputation: 1455
try this in your code
@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();
}
}
Note: from playstore version 10 you dont have to use
mGoogleApiClient.stopAutoManage((Activity) context);
as it will taken care by library it self.
Upvotes: 14
Reputation: 663
This is how I made it for my Fragment:
Code for the fragment:
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 for the fragment:
@Override
public void onStart() {
super.onStart();
mGoogleApiClient.connect();
}
onDestroy for the fragment:
@Override
public void onDestroy() {
super.onDestroy();
mGoogleApiClient.stopAutoManage(getActivity());
mGoogleApiClient.disconnect();
}
I run the buildGoogleApiClient() method in onCreate()
Then I use a MapView in my XML for my fragment as follows:
<com.google.android.gms.maps.MapView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/map" />
Then in my fragment when onCreateView I do the following:
@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;
}
If the navigation is made with a navigationdrawer I did get your problem when I loaded the fragment and then pressed the navigation menu item again and then it crashed so to solve that i did this:
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;
}
Upvotes: 0