【发布时间】:2019-08-28 03:55:12
【问题描述】:
我正在使用 autocomplete_fragment 来允许我的用户搜索地点。单击片段后,它会打开搜索菜单,您可以输入并显示建议。但是,单击任何位置后,“onPlaceSelected”方法将不会运行。之后点击片段也不会做出响应。
此片段位于带有地图的活动中。我尝试在另一个只有文本的活动上使用相同的片段(相同的代码和相同的布局)并且它按预期工作。导致此行为发生的活动中可能发生了什么?
这是显示 onCreate 方法的代码,它调用了一些我不希望成为问题根源的其他方法。
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
// Initialize Places.
Places.initialize(getApplicationContext(), "myapikey");
// Create a new Places client instance.
PlacesClient placesClient = Places.createClient(this);
// Initialize the AutocompleteSupportFragment.
autocompleteFragment = (AutocompleteSupportFragment)
getSupportFragmentManager().findFragmentById(R.id.autocomplete_fragment);
// Specify the types of place data to return.
autocompleteFragment.setPlaceFields(Arrays.asList(Place.Field.ID, Place.Field.NAME));
// Set up a PlaceSelectionListener to handle the response.
autocompleteFragment.setOnPlaceSelectedListener(new PlaceSelectionListener() {
@Override
public void onPlaceSelected(Place place) {
// TODO: Get info about the selected place.
Log.i("CHECK4", "Place: " + place.getName() + ", " + place.getId());
}
@Override
public void onError(Status status) {
// TODO: Handle the error.
Log.i("CHECK4", "An error occurred: " + status);
}
});
// Obtain the SupportMapFragment and get notified when the map is ready to be used.
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapView = mapFragment.getView();
mapFragment.getMapAsync(this);
anchorView = findViewById(R.id.anchorView);
buttonRoutes = findViewById(R.id.buttonRoutes);
buttonMenu = findViewById(R.id.buttonMenu);
buttonRoutes.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
Intent routesIntent = new Intent(MapsActivity.this, RoutesActivity.class);
startActivity(routesIntent);
}
});
buttonMenu.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
Intent routesIntent = new Intent(MapsActivity.this, MenuActivity.class);
startActivity(routesIntent);
}
});
routeIds = getIdsList();
routeTitles = getTitlesList();
routeCamera = getCameraList();
routeZoom = getZoomList();
initializeAllRoutes();
initializeAllStops();
RRdistance = new HashMap<Pair<Integer, Integer>, Double>();
initializeRRdistance();
geocoder = new Geocoder(MapsActivity.this, Locale.getDefault());
checkFirstTime();
}
出于某种原因,此特定活动中的某些内容正在破坏自动完成片段。没有显示任何错误,只是没有任何反应。
编辑:我尝试删除活动的不同部分,直到自动完成片段起作用,并且我设法找到了发生的情况。 OnActivityResult 方法出于某种原因是问题的原因。删除该方法使自动完成片段工作。
【问题讨论】:
-
您还有这个问题吗?您可以尝试隔离自动完成功能以排除其他代码中可能与之冲突的问题吗?
-
您也可以尝试发布您的完整代码,以便我们可以从我们这边复制它吗?
-
@evan 我编辑了帖子以添加我找到的解决方案。
-
感谢您的更新,很高兴听到您的问题现已解决。为了社区的利益,您可能想发布您的解决方案并接受您自己的答案。 :)
标签: android google-maps android-studio googleplacesautocomplete