gpt4 book ai didi

android - IllegalArgumentException:非托管描述符

转载 作者:行者123 更新时间:2023-11-29 18:52:47 26 4
gpt4 key购买 nike

每当有人从 PlaceAutoComplete fragment 中选择一个地点并在 map 上显示标记时,我都会尝试运行地理查询。它第一次工作正常。当我启动应用程序时,图标都很好,地理查询运行正常,但是当我第二次输入一个位置时,应用程序崩溃并显示错误 llegalArgumentException: Unmanaged descriptor以下是我正在尝试做的事情。

public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {

SupportPlaceAutocompleteFragment autocompleteFragment =
(SupportPlaceAutocompleteFragment)

getChildFragmentManager().findFragmentById
(R.id.place_autocomplete_fragment);

autocompleteFragment.setOnPlaceSelectedListener(new
PlaceSelectionListener() {
@Override
public void onPlaceSelected(Place place) {
// TODO: Get info about the selected place.

Toast.makeText(getContext(),place.getAddress(),Toast.LENGTH_LONG).show();
Log.i(TAG, "Place: " + place.getName());

Double latitude1 = place.getLatLng().latitude;
Double longitude1 =place.getLatLng().longitude;
LatLng latLng = new LatLng(latitude1,longitude1);

getPeople(latLng); // method to call geofire query
}

@Override
public void onError(Status status) {
// TODO: Handle the error.
Log.i(TAG, "An error occurred: " + status);
}
});
return mMainView;
}


public void getPeople(LatLng latLng1){
mMap.clear();
mMap.addCircle(new CircleOptions()
.center(latLng1)
.radius(2000)
.strokeColor(Color.BLACK)
.fillColor(0x220000FF)
.strokeWidth(1)
);
DatabaseReference ref =
FirebaseDatabase.getInstance().getReference().child("Location");
GeoFire geoFire = new GeoFire(ref);
GeoQuery geoQuery = geoFire.queryAtLocation(new
GeoLocation(latLng1.latitude, latLng1.longitude), 2);
geoQuery.addGeoQueryEventListener(new GeoQueryEventListener() {
@Override
public void onKeyEntered(final String key, GeoLocation location) {
UIDLocation.put(key,location);
marker.setIcon(BitmapDescriptorFactory.fromResource
(R.drawable.ic_mapmarker2));
markers.put(key, marker);
for (Map.Entry<String,GeoLocation> entry : UIDLocation.entrySet())
{
final Marker marker = markers.get(entry.getKey());
if (marker != null) {
DatabaseReference mUser =
FirebaseDatabase.getInstance().getReference().child("People")
.child(string);
mUser.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot)
{
String display_name =
dataSnapshot.child("name").getValue().toString();
String status =
dataSnapshot.child("status").getValue().toString();
String image =
dataSnapshot.child("image").getValue().toString();
PeopleInfo info = new PeopleInfo();
info.setName(display_name);
info.setStatus(status);
info.setImage(image);
marker.setTag(info);
String iconName = dataSnapshot.child("iconName")
.getValue().toString();
Context context = getContext();
int id = context.getResources().getIdentifier(iconName, "drawable",
context.getPackageName());
String s = String.valueOf(id);

Bitmap icon = BitmapFactory.decodeResource(context.getResources(),id);
marker.setIcon(BitmapDescriptorFactory.fromResource(id));
}

@Override
public void onCancelled(DatabaseError
databaseError) {

}
});
}
}

@Override
public void onMapReady(final GoogleMap googleMap) {
mMap = googleMap;
mUiSettings = mMap.getUiSettings();
mUiSettings.setZoomControlsEnabled(true);
mFusedLocationClient =
LocationServices.getFusedLocationProviderClient(getContext());

Task task= mFusedLocationClient.getLastLocation()
.addOnSuccessListener(getActivity(), new
OnSuccessListener<Location>() {
@Override
public void onSuccess(Location location) {
if (location != null) {
double latitude = location.getLatitude();
double longitude = location.getLongitude();
LatLng latLng = new LatLng(latitude, longitude);
myPosition = new LatLng(latitude, longitude);
markeroptn = new MarkerOptions();
markeroptn.position(myPosition);
markeroptn.title("You are Here");
mMap.moveCamera(CameraUpdateFactory.newLatLng(myPosition));
mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(myPosition,10));
getWorkMen(myPosition);



}
}
});

到目前为止,我从 SO 上的帖子中了解到,这是因为程序试图在已经存在的标记上设置图标。我在 getPeople() 的开头尝试了 clear() map 但它仍然显示相同的错误。它第一次工作正常。我也尝试了 remove() 但它也不起作用。

最佳答案

问题可能出在您管理 Marker 变量的方式上。在代码中,您将 marker 变量存储为方法范围之外的全局变量。当调用 map.clear() 时,它会使 marker 变量无效,如果您仍以某种方式使用此变量来设置某些内容,则可能会导致异常。用于映射 keyMarkermarkers Map 也会发生同样的事情,当 map.clear() 时它不会被清除

尝试更仔细地管理您的 map 元素,独立地清除每个 map 元素并避免使用 map.clear()

建议方式:

创建新标记

private void addMarker(String key, LatLng latLng) {
// Clear the current marker before add the new one
if (marker != null) {
marker.remove();
marker = null;
}

// Store new marker to the variable
marker = mMap.addCircle(new CircleOptions()
.center(latLng)
.radius(2000)
.strokeColor(Color.BLACK)
.fillColor(0x220000FF)
.strokeWidth(1)
);

// Add to markers map if needed
markers.put(key, marker);
}

清除所有标记(手动清除每个可用的标记变量,不要使用map.clear:

public synchronized void clear() {
// markers is the marker map
for (Marker marker : markers.values()) {
try {
marker.remove();
} catch (IllegalArgumentException ex) {
ex.printStackTrace();
}
}

// Clear all the marker map
markers.clear();

// Marker is the your global marker variable
marker.remove();
}

关于android - IllegalArgumentException:非托管描述符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50568276/

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