Próbuję umieścić PlaceAutocomplete w szufladzie nawigacji. Również każdy projekt z api placeautocomplete opublikowany na github może być odpowiedzią lub dowolnym źródłem, jak zrobić niestandardową szufladę nav z placeautocomplete

Oto mój kod:

Wyszukaj układ fragmentów:

<FrameLayout
android:id="@+id/autocompleteHolder"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/customViewPager"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">

<fragment
    android:id="@+id/autocomplete_fragment"
    android:name="com.x.egerg.fragments.AutoComplete"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

Drawerlayout.

    <include
        layout="@layout/app_bar_main"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <android.support.design.widget.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:fitsSystemWindows="true"
        app:headerLayout="@layout/searchfraglay"
        app:menu="@menu/activity_main_drawer" />

</android.support.v4.widget.DrawerLayout>

Fragment autouzupełniania

class AutoComplete : Fragment(), PlaceSelectionListener {
override fun onCreate(savedInstanceState: Bundle?) {
    val autocompleteFragment = fragmentManager!!.findFragmentById(R.id.autocomplete_fragment) as SupportPlaceAutocompleteFragment
    autocompleteFragment.setOnPlaceSelectedListener(this)
    super.onCreate(savedInstanceState)
}
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
                          savedInstanceState: Bundle?): View? {

    return inflater.inflate(R.layout.searchfraglay, container, false)
    }


override fun onPlaceSelected(p0: Place?) {
    Log.d("OnPlaceSelected", p0?.name.toString())
}

override fun onError(p0: Status?) {
    Log.d("OnPlaceSelected", "error")

}

}

Wewnątrz MainActivity

nav_view.inflateHeaderView(R.layout.searchfraglay)

    val ft = supportFragmentManager.beginTransaction()
    ft.replace(R.id.autocompleteHolder, AutoComplete())
    ft.commit()

Logcat

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.x.egerg/com.x.egerg.MainActivity}: android.view.InflateException: Binary XML file line #11: Binary XML file line #11: Error inflating class fragment
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2778)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856)
    at android.app.ActivityThread.-wrap11(Unknown Source:0)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589)
    at android.os.Handler.dispatchMessage(Handler.java:106)
    at android.os.Looper.loop(Looper.java:164)
    at android.app.ActivityThread.main(ActivityThread.java:6494)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
 Caused by: android.view.InflateException: Binary XML file line #11: Binary XML file line #11: Error inflating class fragment
 Caused by: android.view.InflateException: Binary XML file line #11: Error inflating class fragment
 Caused by: java.lang.ClassCastException: com.x.egerg.fragments.AutoComplete cannot be cast to com.google.android.gms.location.places.ui.SupportPlaceAutocompleteFragment
    at com.x.egerg.fragments.AutoComplete.onCreate(AutoComplete.kt:30)
   xxx
    at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:824)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:515)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
    at android.support.design.internal.NavigationMenuPresenter.inflateHeaderView(NavigationMenuPresenter.java:211)
    at android.support.design.widget.NavigationView.inflateHeaderView(NavigationView.java:267)
    at com.x.egerg.MainActivity.onCreate(MainActivity.kt:21)
0
Jurgiele 20 listopad 2018, 03:15

1 odpowiedź

Najlepsza odpowiedź

Powinienem zaimplementować PlaceSelectionListener wewnątrz MainActivity nie we fragmentach.

0
Jurgiele 20 listopad 2018, 04:39