未触发SupportPlaceAutocompleteFragment的OnPlaceSelectedListener

时间:2016-07-04 08:59:18

标签: android android-fragments android-viewpager google-maps-android-api-2 google-places-api

我对 SupportPlaceAutocompleteFragment OnPlaceSelectedListener 方法有疑问。

我的 onViewCreated()方法:

@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);

    // Initialise a new fragment inside of this one.
    mFragmentManager = getChildFragmentManager();
    mSupportMapFragment = (SupportMapFragment) mFragmentManager.findFragmentByTag("mapFragment");
    mSupportPlaceAutocompleteFragment = (SupportPlaceAutocompleteFragment) mFragmentManager.findFragmentByTag("autocompleteFragment");

    // Never inflate fragments inside other fragments in a layout.xml!
    // Do it programmatically.
    // See here for reference: https://stackoverflow.com/a/19815266/4938112.
    if (mSupportMapFragment == null) {
        mSupportMapFragment = new SupportMapFragment();
        fragmentTransaction(mFragmentManager, mSupportMapFragment, R.id.map_fragment_container, "mapFragment");
    }

    // Asynchronous thread to load map.
    mSupportMapFragment.getMapAsync(this);


    if (mSupportPlaceAutocompleteFragment == null) {
        mSupportPlaceAutocompleteFragment = new SupportPlaceAutocompleteFragment();
        fragmentTransaction(mFragmentManager, mSupportPlaceAutocompleteFragment, R.id.card_view, "autocompleteFragment");
    }

    // Filter for a specific place type.
    AutocompleteFilter typeFilter = new AutocompleteFilter.Builder()
            .setTypeFilter(AutocompleteFilter.TYPE_FILTER_CITIES)
            .build();
    mSupportPlaceAutocompleteFragment.setFilter(typeFilter);

    Log.d("I'M HERE", "Hello.");

    mSupportPlaceAutocompleteFragment.setOnPlaceSelectedListener(new PlaceSelectionListener() {
        @Override
        public void onPlaceSelected(Place place) {
            // TODO: Get info about the selected place.
            Log.i("PLACE", "Place: " + place.getName());
            int flag = 1;
            Log.d("FLAG", "flag: " + flag);
        }

        @Override
        public void onError(Status status) {
            // TODO: Handle the error.
            Log.i("PLACE_ERROR", "An error occurred: " + status);
            int flag = 0;
            Log.d("FLAG", "flag: " + flag);
        }
    });

    Log.d("I'M HERE, TOO", "Hello.");

}

当我选择一个地点(启用所有API并且我已经使用Google Key)时,AutoCompleteFragment刚刚关闭,地图上没有任何内容。 不会触发 mSupportPlaceAutocompleteFragment.setOnPlaceSelectedListener(...)方法。

任何提示?

我的问题类似于this question中的问题。

我也使用嵌套片段,在这种情况下,我无法理解如何查看 onActivityResult()方法。

1 个答案:

答案 0 :(得分:0)

终于解决了这个问题:希望它能帮助别人。

我在MainActivity中的 onActivityResult()方法中添加了这个臭名昭着的行

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    ...
}

然后在我的MapFragment中放了相同的方法

    @Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
}

现在嵌套片段正在发送回数据。