google-places-api - 我已经在地图 fagment 上集成了 AutocompleteSupportFragment。当我点击地点搜索时。应用程序崩溃了
问题描述
我已经在地图 fagment 上集成了 AutocompleteSupportFragment。当我点击地点搜索时。应用程序崩溃了。它给出了error.java.lang.NullPointerException:必须设置地点字段。
AutocompleteSupportFragment autocompleteFragment = (AutocompleteSupportFragment)
getChildFragmentManager().findFragmentById(R.id.autocomplete_frag);
if (autocompleteFragment != null) {
autocompleteFragment.setPlaceFields(Arrays.asList(Place.Field.ID, Place.Field.NAME));
}
if (autocompleteFragment != null) {
autocompleteFragment.setOnPlaceSelectedListener(new PlaceSelectionListener() {
@Override
public void onPlaceSelected(@NonNull Place place) {
// TODO: Get info about the selected place.
Toast.makeText(getContext(),place.getName(),Toast.LENGTH_SHORT).show();
}
@Override
public void onError(@NonNull Status status) {
// TODO: Handle the error.
Toast.makeText(getContext(),"Error",Toast.LENGTH_SHORT).show();
}
});
}
解决方案
我有同样的问题,谷歌开发者文档没有告诉你初始化 Places 所需的步骤(为什么他们会包括那个......),但你需要做的就是在初始化autoCompleteFragment
变量的地方添加以下代码片段.
if (!Places.isInitialized()) {
Places.initialize(getApplicationContext(), "YOUR_API_KEY);
}
干杯
推荐阅读
- rust - 将对结构属性的引用绑定到返回可变引用的函数内的变量
- postgresql - 无法使用 Ansible 还原和转储数据库 PostgreSQL
- java - 找到多个文件,其独立于操作系统的路径为“OSGI-OPT/src/org/kohsuke/args4j/spi/URLOptionHandler.java”
- angular - angular material 2 自定义组件使用 mat-auto-complete
- python - ImportError:没有名为 pip._internal 的模块
- asp.net - 引导更新仅适用于调试配置 (Visual Studio)
- c - 如何在 C 中将指令定义为其他指令?
- java - 如何只显示第一列和第一行?
- node.js - 使用 Node.js 从连接的 FTP 下载文件
- vba - Excel VBA - 单击页面上的 Javascript 按钮