简体   繁体   English

导航抽屉中的PlaceAutocomplete

[英]PlaceAutocomplete inside Navigation Drawer

I'm trying to put PlaceAutocomplete inside Navigation Drawer. 我试图将PlaceAutocomplete放入导航抽屉中。 Also any project with placeautocomplete api posted on github can be an answer or any source how to make custom nav drawer with placeautocomplete 此外,任何在github上发布有placeautocomplete api的项目都可以作为答案或任何来源,如何使用placeautocomplete制作自定义导航抽屉

Here is my code: 这是我的代码:

Search fragment layout: 搜索片段布局:

<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 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>

AutoComplete fragment 自动完成片段

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")

}

} }

inside MainActivity 在MainActivity内部

nav_view.inflateHeaderView(R.layout.searchfraglay)

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

Logcat 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)

我应该在MainActivity内实现PlaceSelectionListener ,而不是分段实现。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在“选项卡”菜单中使用“导航抽屉” - How to use Navigation Drawer inside Tabs menu 如何在片段中制作导航抽屉? - how to make a navigation drawer inside fragment? 无法在导航抽屉片段中包含RecyclerView - Cannot include a RecyclerView inside a Navigation Drawer Fragment 在导航抽屉片段内滑动选项卡 - Swiping Tabs inside a Navigation Drawer Fragment 如何将数据从活动传递到导航抽屉android中的片段 - How to pass data from an activity to a fragment inside a navigation drawer android 将带有片段的 Tablayout 作为子片段嵌套在导航抽屉中的片段内 - Nesting a Tablayout With Fragments as Child fragments inside an Fragment from Navigation drawer 在导航抽屉中管理片段中的线程(用于平滑动画) - Managing Threads in Fragment inside Navigation Drawer (for smooth animation) onDrawer打开导航抽屉并在导航抽屉上设置textView - onDrawerOpened Navigation Drawer and setting textView on Navigation Drawer 从导航抽屉中选择其他选项并返回后,导航抽屉内的 VIew pager 不显示片段 - VIew pager inside navigation drawer doesnt show fragment after selecting other option from navigation drawer and coming back 导航抽屉和Google地图 - Navigation Drawer and Google Maps
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM