将AutocompleteFragment放在另一个片段中?

时间:2018-07-18 06:33:52

标签: android android-layout android-fragments google-places-api googleplacesautocomplete

我当前正在尝试在PlacesFragment中显示一个PlaceAutocomplete搜索栏。到目前为止,在PlacesFragment中,我有:

    @Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    PlaceAutocompleteFragment placeAutocompleteFragment  = (PlaceAutocompleteFragment) getActivity().getFragmentManager().findFragmentById(R.id.place_autocomplete_fragment);

    placeAutocompleteFragment.setOnPlaceSelectedListener(
            new PlaceSelectionListener() {
                @Override
                public void onPlaceSelected(Place place) {
                    // TODO: Get info about the selected place.
                    String placeDetailsStr = place.getName() + "\n"
                            + place.getId() + "\n"
                            + place.getLatLng().toString() + "\n"
                            + place.getAddress() + "\n"
                            + place.getAttributions();
                    Log.i("OnPlaceSelected", placeDetailsStr);
                }

                @Override
                public void onError(Status status) {
                    // TODO: Handle the error.
                    Log.i("OnPlaceSelected", "An error occurred: " + status);
                }
            }
    );
}

我的R.id.place_autocomplete_fragment在fragment_places.xml文件中为:

    <fragment
    android:id="@+id/place_autocomplete_fragment"
    android:name="com.google.android.gms.location.places.ui.PlaceAutocompleteFragment"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentStart="true"
    android:layout_marginBottom="109dp" />

但是,我遇到了错误

  

“试图调用虚拟方法'void   com.google.android.gms.location.places.ui.PlaceAutocompleteFragment.setOnPlaceSelectedListener(com.google.android.gms.location.places.ui.PlaceSelectionListener)'   在空对象引用上”。

总的来说,我想知道这是否是在PlacesFragment中接一个PlacesAutocompleteFragment的正确方法,如果不是,我应该怎么做?预先感谢!

1 个答案:

答案 0 :(得分:0)

替换

<fragment
android:id="@+id/place_autocomplete_fragment"
android:name="com.google.android.gms.location.places.ui.PlaceAutocompleteFragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentStart="true"
android:layout_marginBottom="109dp" />

使用

 <FrameLayout
   android:id="@+id/place_autocomplete_fragment_container"
   android:layout_width="match_parent"
   android:layout_height="wrap_content"
   android:layout_alignParentBottom="true"
   android:layout_alignParentStart="true"
   android:layout_marginBottom="109dp" />

现在在PlacesFragment的onViewCreated方法中,

@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
   super.onViewCreated(view, savedInstanceState);
   getChildFragmentManager().beginTransaction()
                    .replace(R.id.place_autocomplete_fragment_container, new PlaceAutocompleteFragment())
                    .addToBackStack(PlaceAutocompleteFragment.class.getName())
                    .commit();

}

在onAttachFragment方法中,

@Override
public void onAttachFragment(Fragment childFragment) {
    super.onAttachFragment(childFragment);
    if(childFragment instanceof PlaceAutocompleteFragment) {
        ((PlaceAutocompleteFragment)childFragment).setOnPlaceSelectedListener(
        new PlaceSelectionListener() {
            @Override
            public void onPlaceSelected(Place place) {
                // TODO: Get info about the selected place.
                String placeDetailsStr = place.getName() + "\n"
                        + place.getId() + "\n"
                        + place.getLatLng().toString() + "\n"
                        + place.getAddress() + "\n"
                        + place.getAttributions();
                Log.i("OnPlaceSelected", placeDetailsStr);
            }

            @Override
            public void onError(Status status) {
                // TODO: Handle the error.
                Log.i("OnPlaceSelected", "An error occurred: " + status);
            }
        }
);
    }


}