Java Android-EBADF(错误文件号)OnClickInfoMarker
我已经搜索了相关问题,但找不到。我创建了一个显示图片、姓名和地址的InfoWindowMarker。然后我创建OnInfo WindowClickListener,它将显示所选标记的纬度和经度。但是当我点击信息窗口时,我得到了这个错误信息 日志: com.xxxxxxxx.xxxxxxxxx E/合子﹕ 合子:关闭描述符时出错 libcore.io.ErrnoException:关闭失败:EBADF(错误的文件号) 在libcore.io.Posix.close中(本机方法) 在libcore.io.BlockGuardOs.close中(BlockGuardOs.java:75) 位于com.android.internal.os.ZygoteInit.closeServerSocket(ZygoteInit.java:221) 位于com.android.internal.os.ZygoteConnection.handleChildProc(ZygoteConnection.java:879) 位于com.android.internal.os.ZygoteConnection.runOnce(ZygoteConnection.java:242) 位于com.android.internal.os.ZygoteInit.runSelectLoop(ZygoteInit.java:713) 位于com.android.internal.os.ZygoteInit.main(ZygoteInit.java:649) 在dalvik.system.NativeStart.main(本机方法) InfoWindowAdapter:Java Android-EBADF(错误文件号)OnClickInfoMarker,java,android,google-maps,Java,Android,Google Maps,我已经搜索了相关问题,但找不到。我创建了一个显示图片、姓名和地址的InfoWindowMarker。然后我创建OnInfo WindowClickListener,它将显示所选标记的纬度和经度。但是当我点击信息窗口时,我得到了这个错误信息 日志: com.xxxxxxxx.xxxxxxxxx E/合子﹕ 合子:关闭描述符时出错 libcore.io.ErrnoException:关闭失败:EBADF(错误的文件号) 在libcore.io.Posix.close中(本机方法) 在libcore.
public GoogleMap.InfoWindowAdapter CustomMarkerInfo = new GoogleMap.InfoWindowAdapter() {
@Override
public View getInfoWindow(Marker marker) {
ViewGroup viewGroup = (ViewGroup)getLayoutInflater().inflate(R.layout.custom_info_window, null);
ImageView ivRow = (ImageView)viewGroup.findViewById(R.id.ivRowImage);
TextView locationName = (TextView)viewGroup.findViewById(R.id.tvRowTitle);
TextView locationAddress = (TextView)viewGroup.findViewById(R.id.tvRowTitle2);
Bitmap locationImage;
locationName.setText(marker.getTitle());
locationAddress.setText(marker.getSnippet());
URL imageURL;
try {
imageURL = new URL(markerInfo.get(marker.getId()));
locationImage = BitmapFactory.decodeStream(imageURL.openConnection().getInputStream());
ivRow.setImageBitmap(locationImage);
}catch (Exception e){
e.printStackTrace();
}
return viewGroup;
}
@Override
public View getInfoContents(Marker marker) {return null;}
};
OnInfo WindowClickListener:
private GoogleMap.OnInfoWindowClickListener onInfoWindowClickListener = new GoogleMap.OnInfoWindowClickListener() {
@Override
public void onInfoWindowClick(Marker marker) {
LatLng myLatLng = myLatLng();
Toast.makeText(getApplicationContext(),myLatLng.toString(),Toast.LENGTH_LONG).show();
}
};
private final LocationListener locationListener = new LocationListener() {
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {}
@Override
public void onProviderEnabled(String provider) {}
@Override
public void onProviderDisabled(String provider) {}
@Override
public void onLocationChanged(Location newLocation) {
}
};
private LatLng myLatLng(){
LocationManager locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
String provider;
Location location;
LatLng myLatLng = null;
if(locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)){
provider = LocationManager.GPS_PROVIDER;
Toast.makeText(getApplicationContext(),"GPS tidak aktif.",Toast.LENGTH_LONG).show();
}else if(locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)){
provider = LocationManager.NETWORK_PROVIDER;
}else{
provider = LocationManager.PASSIVE_PROVIDER;
}
if(locationManager.getLastKnownLocation(provider) == null){
locationManager.requestLocationUpdates(provider,2000,10,locationListener);
location = locationManager.getLastKnownLocation(provider);
if(location == null){
Toast.makeText(getApplicationContext(),"Tidak dapat menemukan lokasi saat ini, coba lagi.",Toast.LENGTH_LONG).show();
}else{
location = locationManager.getLastKnownLocation(provider);
myLatLng = new LatLng(location.getLatitude(),location.getLongitude());
}
}else{
location = locationManager.getLastKnownLocation(provider);
myLatLng = new LatLng(location.getLatitude(),location.getLongitude());
}
return myLatLng == null ? myLatLng() : myLatLng;
}
谢谢你的帮助
更新:从加载的JSON创建标记。如果是,则可能导致此问题。这可能是线程问题。在从URL加载图像之前,将返回您的
视图组
。解决此问题的一个简单方法是使用第三方图像加载库Picasso
示例代码:
ImageView imageView = (ImageView)myContentsView.findViewById(R.id.ivRowImage);
if (not_first_time_showing_info_window) {
Picasso.with(getApplicationContext()).load("YOUR_IMG_URL").into(imageView);
} else { // if it's the first time, load the image with the callback set
not_first_time_showing_info_window=true;
Picasso.with(getApplicationContext()).load("YOUR_IMG_URL").into(imageView,new InfoWindowRefresher(marker));
}
然后您可以使用private helper方法来确保第一次单击中显示的异步:
private class InfoWindowRefresher implements Callback {
private Marker markerToRefresh;
private InfoWindowRefresher(Marker markerToRefresh) {
this.markerToRefresh = markerToRefresh;
}
@Override
public void onSuccess() {
markerToRefresh.showInfoWindow();
}
@Override
public void onError() {}
}
您可以参考此GitHub页面了解完整的实现:
如果您真的想使用ImageLoader,可以查看以下帖子:
(但与使用毕加索从URL加载相比要复杂得多)我尝试使用默认信息窗口,但仍然面临同样的问题。我将尝试在我以前的活动中使用毕加索,该活动使用AsyncTask加载图像。也许它会解决这个问题。谢谢,我把所有的图像加载器都改成毕加索,合子错误消失了。