无法找到PlaceAutocompleteFragment

时间:2018-06-17 18:43:43

标签: android

我在我的Activity中使用了PlaceAutocompleteFragment并且它有效。然后我想将这段代码移到我的片段中,然后停止工作。

在MainActivity中执行我的片段:

FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction().replace(R.id.container, fragment).commit();

ForecastFragment:

public class ForecastFragment extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_forecast, container, false);

        autocompleteFragment = (PlaceAutocompleteFragment)
                getFragmentManager().findFragmentById(R.id.place_autocomplete_fragment);
        AutocompleteFilter typeFilter = new AutocompleteFilter.Builder()
                .setTypeFilter(AutocompleteFilter.TYPE_FILTER_CITIES)
                .build();

        autocompleteFragment.setFilter(typeFilter); //here I have NullPointerException
        //...

        return v;
    }
}

我在 autocompleteFragment.setFilter(typeFilter)

上有NullPointerExceprion

fragment_forecast.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    >
    <fragment
        android:id="@+id/place_autocomplete_fragment"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
         android:name="com.google.android.gms.location.places.ui.PlaceAutocompleteFragment" android:layout_weight="1"/>
</LinearLayout>

Here is my project on GitHub

0 个答案:

没有答案