Android 为什么不是';第一次搜索后是否自动完成碎片响应?
我正在使用一个自动完成的片段来允许我的用户搜索地方。单击片段后,它会打开搜索菜单,您可以键入并显示建议。但是,单击任何位置后,“onPlaceSelected”方法将不会运行。之后,点击片段也不会使其响应 此片段位于具有映射的活动中。我试着在另一个只有文本的活动中使用相同的片段(相同的代码和相同的布局),它按照预期工作。在这项活动中会发生什么使这种行为发生 下面的代码显示了onCreate方法,它调用了一些我不认为是问题根源的其他方法Android 为什么不是';第一次搜索后是否自动完成碎片响应?,android,google-maps,android-studio,googleplacesautocomplete,Android,Google Maps,Android Studio,Googleplacesautocomplete,我正在使用一个自动完成的片段来允许我的用户搜索地方。单击片段后,它会打开搜索菜单,您可以键入并显示建议。但是,单击任何位置后,“onPlaceSelected”方法将不会运行。之后,点击片段也不会使其响应 此片段位于具有映射的活动中。我试着在另一个只有文本的活动中使用相同的片段(相同的代码和相同的布局),它按照预期工作。在这项活动中会发生什么使这种行为发生 下面的代码显示了onCreate方法,它调用了一些我不认为是问题根源的其他方法 protected void onCreate(Bundle
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();
}
创建时受保护的void(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_映射);
//初始化位置。
初始化(getApplicationContext(),“myapikey”);
//创建一个新的Places客户端实例。
PlacesClient PlacesClient=Places.createClient(此);
//初始化AutocompleteSupportFragment。
autocompleteFragment=(AutocompleteSupportFragment)
getSupportFragmentManager().findFragmentById(R.id.autocomplete_fragment);
//指定要返回的位置数据的类型。
setPlaceFields(Arrays.asList(Place.Field.ID,Place.Field.NAME));
//设置PlaceSelectionListener来处理响应。
autocompleteFragment.setOnPlaceSelectedListener(新的PlaceSelectionListener(){
@凌驾
已选定地点上的公共作废(地点){
//TODO:获取有关所选地点的信息。
Log.i(“CHECK4”,“Place:+Place.getName()+”,“+Place.getId());
}
@凌驾
公共作废申报人(状态){
//TODO:处理错误。
Log.i(“检查4”,“出现错误:+状态”);
}
});
//获取SupportMapFragment,并在地图准备好使用时收到通知。
SupportMapFragment mapFragment=(SupportMapFragment)getSupportFragmentManager()
.findFragmentById(R.id.map);
mapView=mapFragment.getView();
getMapAsync(这个);
anchorView=findViewById(R.id.anchorView);
buttonRoutes=findViewById(R.id.buttonRoutes);
buttonMenu=findViewById(R.id.buttonMenu);
buttonRoutes.setOnClickListener(新视图.OnClickListener()
{
@凌驾
公共void onClick(视图v)
{
Intent routesIntent=新的Intent(MapsActivity.this、RouteActivity.class);
startActivity(路线内容);
}
});
buttonMenu.setOnClickListener(新视图.OnClickListener()
{
@凌驾
公共void onClick(视图v)
{
Intent routesIntent=新的Intent(MapsActivity.this、menuaActivity.class);
startActivity(路线内容);
}
});
routeIds=getIdsList();
routeTitles=getTitlesList();
routeCamera=getCameraList();
routeZoom=getZoomList();
初始化所有路由();
初始化所有操作();
RRdistance=newhashmap();
初始化器距离();
geocoder=新的geocoder(MapsActivity.this,Locale.getDefault());
checkFirstTime();
}
出于某种原因,在这个特定的活动中有一些东西正在破坏自动完成片段。没有显示错误,只是什么也没发生
编辑:我尝试删除我活动的不同部分,直到自动完成片段起作用,我设法找到了发生的事情。由于某种原因,OnActivityResult方法是问题的原因。删除该方法使自动完成片段工作。由于某种原因,OnActivityResult方法是问题的原因。删除该方法使自动完成片段工作。在迁移到new places API之前,我使用了此方法。由于某种原因,OnActivityResult方法是问题的原因。删除该方法使自动完成片段工作。在迁移到new places API之前,我使用了此方法。您是否仍然存在此问题?您能否尝试隔离Autocomplete以排除您的其他代码中可能与之冲突的问题?您能否尝试发布完整代码,以便我们可以从我们这边复制此内容?@evan我编辑了此帖子以添加我找到的解决方案。感谢您的更新,很高兴听到您的问题现已修复。为了社区的利益,您可能希望发布您的解决方案并接受您自己的答案。:)你还有这个问题吗?您能否尝试隔离Autocomplete以排除您的其他代码中可能与之冲突的问题?您能否尝试发布完整代码,以便我们可以从我们这边复制此内容?@evan我编辑了此帖子以添加我找到的解决方案。感谢您的更新,很高兴听到您的问题现已修复。为了社区的利益,您可能希望发布您的解决方案并接受您自己的答案。:)