Android IllegalArgumentException:非托管描述符
每当有人从PlaceAutoComplete片段中选择一个地方并在地图上显示标记时,我都会尝试运行地理查询。第一次运行正常。当我启动应用程序时,图标都正常,geoquery运行正常,但当我第二次进入某个位置时,应用程序崩溃,显示错误llegalArgumentException:Unmanaged descriptor,下面是我正在尝试做的Android IllegalArgumentException:非托管描述符,android,google-maps,google-maps-markers,illegalargumentexception,Android,Google Maps,Google Maps Markers,Illegalargumentexception,每当有人从PlaceAutoComplete片段中选择一个地方并在地图上显示标记时,我都会尝试运行地理查询。第一次运行正常。当我启动应用程序时,图标都正常,geoquery运行正常,但当我第二次进入某个位置时,应用程序崩溃,显示错误llegalArgumentException:Unmanaged descriptor,下面是我正在尝试做的 public View onCreateView(LayoutInflater inflater, ViewGroup container,
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);
}
}
});
public View onCreateView(布局、充气机、视图组容器、,
Bundle savedInstanceState){
SupportPlaceAutocompleteFragment autocompleteFragment=
(SupportPlaceAutocompleteFragment)
getChildFragmentManager().findFragmentById
(R.id.place\u autocomplete\u fragment);
autocompleteFragment.setOnPlaceSelectedListener(新建
PlaceSelectionListener(){
@凌驾
已选定地点上的公共作废(地点){
//TODO:获取有关所选地点的信息。
Toast.makeText(getContext(),place.getAddress(),Toast.LENGTH_LONG.show();
Log.i(标记“Place:+Place.getName());
双纬度1=place.getLatLng().纬度;
Double longitude1=place.getLatLng().longitude;
LatLng LatLng=新的LatLng(纬度1,经度1);
getPeople(latLng);//调用geofire查询的方法
}
@凌驾
公共作废申报人(状态){
//TODO:处理错误。
Log.i(标记“发生错误:+状态”);
}
});
返回视图;
}
公共人员(LatLng latLng1){
mMap.clear();
mMap.addCircle(新的CircleOptions()
.中心(latLng1)
.半径(2000年)
.strokeColor(颜色.黑色)
.fillColor(0x220000FF)
.冲程宽度(1)
);
数据库引用ref=
FirebaseDatabase.getInstance().getReference().child(“位置”);
GeoFire GeoFire=新的GeoFire(参考);
GeoQuery GeoQuery=geoFire.queryLocation(新
地理位置(纬度,经度),2;
geoQuery.addGeoQueryEventListener(新的GeoQueryEventListener(){
@凌驾
public void onKeyEntered(最终字符串键,地理位置){
UIDLocation.put(键、位置);
marker.setIcon(BitmapDescriptorFactory.fromResource
(R.drawable.ic_mapmarker2));
放(键,记号笔);
对于(Map.Entry:UIDLocation.entrySet())
{
final Marker=markers.get(entry.getKey());
如果(标记!=null){
数据库引用mUser=
FirebaseDatabase.getInstance().getReference().child(“人员”)
.儿童(字符串);
mUser.addValueEventListener(新的ValueEventListener(){
@凌驾
公共void onDataChange(DataSnapshot DataSnapshot)
{
字符串显示\u名称=
dataSnapshot.child(“name”).getValue().toString();
字符串状态=
dataSnapshot.child(“status”).getValue().toString();
字符串图像=
dataSnapshot.child(“image”).getValue().toString();
PeopleInfo=新建PeopleInfo();
信息集合名(显示名称);
信息设置状态(状态);
info.setImage(图像);
marker.setTag(信息);
字符串iconName=dataSnapshot.child(“iconName”)
.getValue().toString();
Context=getContext();
int id=context.getResources().getIdentifier(iconName,“可绘制”,
getPackageName());
字符串s=字符串.valueOf(id);
位图图标=BitmapFactory.decodeResource(context.getResources(),id);
setIcon(BitmapDescriptorFactory.fromResource(id));
}
@凌驾
已取消的公共void(数据库错误
数据库错误){
}
});
}
}
@凌驾
4月1日公开作废(最终谷歌地图谷歌地图){
mMap=谷歌地图;
mUiSettings=mMap.getUiSettings();
mUiSettings.setZoomControlsEnabled(true);
mFusedLocationClient=
getFusedLocationProviderClient(getContext());
Task Task=mFusedLocationClient.getLastLocation()
.addOnSuccessListener(getActivity(),新建
OnSuccessListener(){
@凌驾
成功时公共无效(位置){
如果(位置!=null){
双纬度=location.getLatitude();
double longitude=location.getLongitude();
LatLng LatLng=新LatLng(纬度、经度);
myPosition=新板条(纬度、经度);
markeroptn=新的MarkerOptions();
标记位置(myPosition);
markeroptn.title(“你在这里”);
mMap.moveCamera(CameraUpdateFactory.newLatLng(myPosition));
mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(myPosition,10));
Getworkers(我的职位);
}
}
});
到目前为止,我从上的帖子中了解到,这是因为程序试图在已经存在的标记上设置图标。我在getPeople()的开头尝试了clear()map,但它仍然显示相同的错误。它第一次工作正常。我还尝试了remove()但它也不起作用。问题可能来自管理
标记
变量的方式。在代码中,您将标记
变量存储为方法范围外的全局变量。调用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);
}
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();
}