【问题标题】:PlaceAutocompleteFragment inside another fragment?PlaceAutocompleteFragment 在另一个片段内?
【发布时间】:2018-07-18 06:33:51
【问题描述】:

我目前正在尝试在 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 的正确方法,如果不是,我应该怎么做?提前致谢!

【问题讨论】:

  • 另外,Android Studio 说“'getFragmentManager()' 已被弃用”并且可能返回 NullPointerException,那么我该如何解决这个问题?
  • 检查 是否在 Activity 布局文件中? &lt;fragment&gt; 仅在其位于活动布局文件中时才有效。
  • 如果您使用支持库,请使用getSupportFragmentManager()
  • 哦,将 移动到主 Activity 布局文件有效!现在,即使我使用 ViewPager 切换,搜索栏也会显示在所有片段中,那么如何仅从 PlacesFragment 显示它?
  • 不要以这种方式使用&lt;fragment&gt;。而是将其替换为 &lt;FrameLayout&gt; 并在那里绑定片段,就像您首先完成的那样。只有 id 会有所不同,您必须创建一个 PlaceAutocompleteFragment 对象。

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


【解决方案1】:

替换

<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);
            }
        }
);
    }


}

【讨论】:

  • 谢谢!似乎 onPlaceSelected 在我选择一个地点后从未执行过……知道为什么吗?
猜你喜欢
  • 2021-06-26
  • 1970-01-01
  • 1970-01-01
  • 2023-04-11
  • 2017-03-25
  • 2016-09-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多