复用GoogleMap类时双地图调用setEditSearchAPIListener崩溃问题
Hey there, let's break down this problem and fix it step by step.
Why This Crash Happens
The error "Already managing a GoogleApiClient with id 0 inside a Java Class" means your reusable GoogleMap class is likely creating and managing a single GoogleApiClient instance internally. When you initialize the second map (map2) and call setEditSearchAPIListener, it tries to register another instance of GoogleApiClient with the same ID—and Google Play Services doesn't allow multiple instances to manage the same client ID simultaneously.
Fixes to Try
1. Move GoogleApiClient Management to the Fragment Level
Instead of letting each map instance handle its own GoogleApiClient, create and manage a single client in your PostAJobFragment. This way both maps can share the same client without conflicts.
2. Adjust Your Reusable GoogleMap Class
Modify your reusable class to accept an external GoogleApiClient (or modern PlacesClient) instead of creating one internally. Here's a quick example:
public class ReusableMapHandler { private GoogleApiClient mSharedApiClient; private AutoCompleteTextView mSearchInput; // Constructor accepts the shared GoogleApiClient from the Fragment public ReusableMapHandler(Context context, GoogleApiClient sharedApiClient) { mSharedApiClient = sharedApiClient; // Rest of your map initialization logic } public void setEditSearchAPIListener(AutoCompleteTextView searchInput) { mSearchInput = searchInput; // Use the shared client to set up place autocomplete Places.initialize(context, YOUR_API_KEY); PlacesAutocompleteAdapter adapter = new PlacesAutocompleteAdapter( context, mSharedApiClient, null, null ); mSearchInput.setAdapter(adapter); // Add your search item click listener logic here } }
3. Manage the Client's Lifecycle in the Fragment
Make sure you properly connect/disconnect the GoogleApiClient in your Fragment's lifecycle methods to avoid leaks or invalid states:
public class PostAJobFragment extends Fragment { private GoogleApiClient mGoogleApiClient; private ReusableMapHandler map1Handler; private ReusableMapHandler map2Handler; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // Initialize the shared GoogleApiClient mGoogleApiClient = new GoogleApiClient.Builder(requireContext()) .addApi(Places.GEO_DATA_API) .addApi(Places.PLACE_DETECTION_API) .build(); } @Override public void onStart() { super.onStart(); if (mGoogleApiClient != null) { mGoogleApiClient.connect(); } } @Override public void onStop() { super.onStop(); if (mGoogleApiClient != null && mGoogleApiClient.isConnected()) { mGoogleApiClient.disconnect(); } } // Initialize your two maps using the shared client private void initMaps() { map1Handler = new ReusableMapHandler(requireContext(), mGoogleApiClient); map2Handler = new ReusableMapHandler(requireContext(), mGoogleApiClient); // Set up search listeners for both maps map1Handler.setEditSearchAPIListener(editTextSearchPickup); map2Handler.setEditSearchAPIListener(editTextSearchDelivery); } }
4. Use Modern Places Client (If Possible)
If you're using a newer version of Google Play Services, consider switching to PlacesClient (a replacement for the old GoogleApiClient). It's designed to be a singleton, so multiple map instances can safely share it without conflicts:
// In your Fragment PlacesClient placesClient = Places.createClient(requireContext()); // Pass this to your reusable map class instead of GoogleApiClient
内容的提问来源于stack exchange,提问作者javacoder123

