【问题标题】:How to call SupportPlaceAutocompleteFragment inside onCreateView()如何在 onCreateView() 中调用 SupportPlaceAutocompleteFragment
【发布时间】:2016-11-11 14:17:40
【问题描述】:

我在我的应用程序中使用 getChildFragmentManager(),我想在 Fragment OncreateView() 中调用下面的代码。任何人都可以帮助实现此功能,实际上我已经保留了代码,我在 Line 中遇到错误2号?

   1.  SupportPlaceAutocompleteFragment autocompleteFragment = (SupportPlaceAutocompleteFragment) getFragmentManager()
   2.                 .findFragmentById(R.id.place_autocomplete_fragment);
   3.         autocompleteFragment.setOnPlaceSelectedListener(this);
   4.         autocompleteFragment.setHint("Select a Location");

错误: java.lang.NullPointerException:尝试调用虚拟方法 'void com.google.android.gms.location.places.ui.SupportPlaceAutocompleteFragment.setOnPlaceSelectedListener(com.google. android.gms.location.places.ui.PlaceSelectionListener)' 在空对象引用上 com.example.sample.fragments.LocationFragment.onCreateView(LocationFragment.java:2)

                                                                        at 

【问题讨论】:

    标签: android google-maps google-places-api


    【解决方案1】:

    当您使用SupportPlaceAutocompleteFragment 时,您的活动应该实现AppCompatActivity(或FragmentActivity 并使用getSupportFragmentManager())来访问您的片段。

    或者反过来,创建一个PlaceAutoCompleteFragment 而不是SupportPlaceAutoCompleteFragment

    在此处检查选项 #1 的 支持 部分 https://developers.google.com/places/android-api/autocomplete

    ps。我假设您的布局 xml 中已经有 R.id.place_autocomplete_fragment

    【讨论】:

      猜你喜欢
      • 2017-11-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-02-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多