Android 如何从菜单项触发Google PlaceAutocompleteFragment?

Android 如何从菜单项触发Google PlaceAutocompleteFragment?,android,autocomplete,google-places-api,googleplacesautocomplete,Android,Autocomplete,Google Places Api,Googleplacesautocomplete,我是android新手,正在尝试创建一个列表详细信息应用程序。我正在尝试添加PlaceAutocompleteFragment,用于搜索并将位置添加到我的列表中 现在,我想将PlaceAutocompleteFragment添加到我的应用程序中,并以这样的方式触发它:如果我在actionbar的菜单项上单击加号(“+”),就会调用搜索对话框 相反,我只能设法将PlaceAutocompleteFragment添加到列表活动的布局中 城市列表.xml <?xml version="1.0"

我是android新手,正在尝试创建一个列表详细信息应用程序。我正在尝试添加PlaceAutocompleteFragment,用于搜索并将位置添加到我的列表中

现在,我想将PlaceAutocompleteFragment添加到我的应用程序中,并以这样的方式触发它:如果我在actionbar的菜单项上单击加号(“+”),就会调用搜索对话框

相反,我只能设法将PlaceAutocompleteFragment添加到列表活动的布局中

城市列表.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:android="http://schemas.android.com/apk/res/android" >

    <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.support.v7.widget.RecyclerView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/city_list"
    android:name="com.apoorv.android.weatherapp.CityListFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginLeft="16dp"
    android:layout_marginRight="16dp"
    app:layoutManager="LinearLayoutManager"
    tools:context="com.apoorv.android.weatherapp.CityListActivity"
    tools:listitem="@layout/city_list_content" />
</LinearLayout>
enter code here

然而,我尝试将其添加到我的活动的菜单中,但如果我将片段添加到菜单中,则会导致应用程序崩溃,并出现空指针异常

 <?xml version="1.0" encoding="utf-8"?>
<menu xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:id="@+id/add_city_button"
        android:icon="@android:drawable/ic_input_add"
        android:title="Add City"
        app:showAsAction="ifRoom" />
    <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"
        />
</menu>

我想在单击此菜单的+符号时调用“自动完成”对话框


您是否找到了解决方案?您是否找到了解决方案?