gpt4 book ai didi

java - 未选择任何内容时,PlacePicker Activity 不会调用 `onActivityResult`

转载 作者:行者123 更新时间:2023-11-30 08:51:51 25 4
gpt4 key购买 nike

这是我试过的代码,但是当用户没有选择任何东西而只是返回时, Activity 不会调用 onActivityResult() 方法

我将 PlacePIcker 称为

googleMap.setOnMapClickListener(new GoogleMap.OnMapClickListener() {
@Override
public void onMapClick(LatLng latLng) {
try {
googleMap.clear();
PlacePicker.IntentBuilder intentBuilder =
new PlacePicker.IntentBuilder();
Intent intent = intentBuilder.build(getApplicationContext());
startActivityForResult(intent, 5);
} catch (GooglePlayServicesRepairableException e) {
e.printStackTrace();
} catch (GooglePlayServicesNotAvailableException e) {
e.printStackTrace();
}
}
});

onActivityResult() 方法

if (requestCode == 5 && resultCode == RESULT_OK) {
Toast.makeText(this,"here",Toast.LENGTH_SHORT).show();
if (data != null) {
Place place = PlacePicker.getPlace(data, this);
if (place != null) {
lat = place.getLatLng().latitude;
lng = place.getLatLng().longitude;
MarkerOptions marker = new MarkerOptions().position(
place.getLatLng()).title("Hello Maps"); marker.icon(BitmapDescriptorFactory
.defaultMarker(BitmapDescriptorFactory.HUE_RED));
googleMap.addMarker(marker);
} else{
Toast.makeText(this,"place",Toast.LENGTH_SHORT).show();
MarkerOptions marker = new MarkerOptions().position(
new LatLng(latitude, longitude)).title("Hello Maps");
lat = latitude;
lng = longitude;
// Changing marker icon
marker.icon(BitmapDescriptorFactory
.defaultMarker(BitmapDescriptorFactory.HUE_ROSE));

// adding marker
googleMap.addMarker(marker);
}
} else{
Toast.makeText(this,"data",Toast.LENGTH_SHORT).show();
MarkerOptions marker = new MarkerOptions().position(
new LatLng(latitude, longitude)).title("Hello Maps");
lat = latitude;
lng = longitude;
// Changing marker icon
marker.icon(BitmapDescriptorFactory
.defaultMarker(BitmapDescriptorFactory.HUE_ROSE));

// adding marker
googleMap.addMarker(marker);
}

当我单击 placepicker Activity 中的默认后退按钮时,我没有看到任何 toast 被调用

根据文档,它应该将 place.getplace() 返回为 null

When the user selects a place, you can retrieve the place by calling PlacePicker.getPlace(). If the user has not selected a place, the method will return null.

这意味着它应该转到 onActivityResult(),但在我的例子中没有选择任何它不会到达 onActivityResult() 的地方。

谁能帮我解决这个问题??

最佳答案

我想你漏掉了 else 语句

    if (requestCode == 5 && resultCode == RESULT_OK) {
Toast.makeText(this,"here",Toast.LENGTH_SHORT).show();
if (data != null) {
Place place = PlacePicker.getPlace(data, this);
if (place != null) {
lat = place.getLatLng().latitude;
lng = place.getLatLng().longitude;
MarkerOptions marker = new MarkerOptions().position(
place.getLatLng()).title("Hello Maps"); marker.icon(BitmapDescriptorFactory
.defaultMarker(BitmapDescriptorFactory.HUE_RED));
googleMap.addMarker(marker);
} else{
Toast.makeText(this,"place",Toast.LENGTH_SHORT).show();
MarkerOptions marker = new MarkerOptions().position(
new LatLng(latitude, longitude)).title("Hello Maps");
lat = latitude;
lng = longitude;
// Changing marker icon
marker.icon(BitmapDescriptorFactory
.defaultMarker(BitmapDescriptorFactory.HUE_ROSE));

// adding marker
googleMap.addMarker(marker);
}
} else{
Toast.makeText(this,"data",Toast.LENGTH_SHORT).show();
MarkerOptions marker = new MarkerOptions().position(
new LatLng(latitude, longitude)).title("Hello Maps");
lat = latitude;
lng = longitude;
// Changing marker icon
marker.icon(BitmapDescriptorFactory
.defaultMarker(BitmapDescriptorFactory.HUE_ROSE));

// adding marker
googleMap.addMarker(marker);
}
}else if(requestCode == 5 && resultCode == RESULT_CANCELED){
// handle no selection here
}

关于java - 未选择任何内容时,PlacePicker Activity 不会调用 `onActivityResult`,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30413748/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com