已经在Java类中管理ID为0的GoogleApiClient

时间:2018-04-08 18:44:30

标签: android google-maps fragment google-api-client

我在java类中创建了一个GoogleMap,因此我可以多次重复使用它而无需重复代码。

我目前在同一个片段上有两张地图。一个地图,供用户选择一个收集位置(从中拾取的物品)。第二张地图用于交货地点(物品将到达)。我的两张地图都有效,但是当我尝试添加这一行时,页面上的第二张地图崩溃了:

Inside Fragment,PostAJobFragment

// editTextSearch is an AutoCompleteTextView
map2.setEditSearchAPIListener(editTextSearch2, getActivity());

我创建了一个map实例,并在我的Java Class中访问了setEditSearchAPIListener方法。具体如下:

内部Java类,地图

public GoogleApiClient setEditSearchAPIListener(AutoCompleteTextView editTextSearch, FragmentActivity fragmentActivity)
    {
        mGoogleApiClient1 = new GoogleApiClient
                .Builder(fragmentActivity)
                .addApi(Places.GEO_DATA_API)
                .addApi(Places.PLACE_DETECTION_API)
                .enableAutoManage(fragmentActivity, this)
                .build();

        editTextSearch.setOnItemClickListener(mAutocompleteItemClickListener);
        placeAutocompleteAdapter = new PlaceAutocompleteAdapter(fragmentActivity, mGoogleApiClient1, LAT_LNG_BOUNDS, null);
        editTextSearch.setAdapter(placeAutocompleteAdapter);

        return mGoogleApiClient1;
    }

    @Override
    public void onPause() {
        super.onPause();
        mGoogleApiClient1.stopAutoManage(getActivity());
        mGoogleApiClient1.disconnect();
    }

我收到错误'java.lang.IllegalStateException: Already managing a GoogleApiClient with id 0'

我已经讨论过使用onPause方法的其他答案。由于我的Map使用Java类而不是片段,因此它似乎不起作用。 有什么方法可以解决这个问题吗?

1 个答案:

答案 0 :(得分:0)

我已解决了我的问题。在创建GoogleApiClient时,我尝试在Java类中创建它在一个名为setGoogleApiClient的方法中。它会成功运行一次,但第二次会崩溃。

我把代码放在我之前片段中的onCreateView方法中,它运行正常。

@Override
    public View onCreateView(final LayoutInflater inflater, final ViewGroup container,
                             Bundle savedInstanceState)
    {
        final View view = inflater.inflate(R.layout.fragment_post_an_advert, container, false);
        layoutView = view;

        // Collection Map
        map1 = new Map();
        map2 = new Map();
        getViewsByIds(view);

        // Create google api client, so user has pre-set options to select.
        /*
      The main entry point for Google Play services integration
     */
       mGoogleApiClient1 = new GoogleApiClient
                .Builder(getActivity())
                .addApi(Places.GEO_DATA_API)
                .addApi(Places.PLACE_DETECTION_API)
                .enableAutoManage(getActivity(), 0, this)
                .build();

        PlaceAutocompleteAdapter placeAutocompleteAdapter = new PlaceAutocompleteAdapter(getActivity(), mGoogleApiClient1, LAT_LNG_BOUNDS, null);
        map1.setPlaceAutocompleteAdapter(placeAutocompleteAdapter);
        map1.setmGoogleApiClient1(mGoogleApiClient1);

        map2.setPlaceAutocompleteAdapter(placeAutocompleteAdapter);
        map2.setmGoogleApiClient1(mGoogleApiClient1);

        editTextSearch.setOnItemClickListener(map1.mAutocompleteItemClickListener);
        editTextSearch2.setOnItemClickListener(map2.mAutocompleteItemClickListener);

        editTextSearch.setAdapter(placeAutocompleteAdapter);
        editTextSearch2.setAdapter(placeAutocompleteAdapter);