Android - EBADF(坏文件编号)OnClickInfoMarker [英] Android - EBADF (Bad file number) OnClickInfoMarker

查看:142
本文介绍了Android - EBADF(坏文件编号)OnClickInfoMarker的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经搜索了相关的问题,但找不到它。我创建了一个显示图片,名称和地址的InfoWindowMarker。然后我创建OnInfoWindowClickListener,它将显示所选标记的纬度和经度。但是,当我点击信息窗口,我得到了这个错误信息。



Logcat:


com.xxxxxxxx.xxxxxxxxxx E / Zygote:Zygote:错误关闭描述符
libcore.io.ErrnoException:关闭失败:EBADF(错误文件号)$ b $ libbore.io.Posix.close(Native Method )
at libcore.io.BlockGuardOs.close(BlockGuardOs.java:75)
at com.android.internal.os.ZygoteInit.closeServerSocket(ZygoteInit.java:221)
at com。 android.internal.os.ZygoteConnection.handleChildProc(ZygoteConnection.java:879)
at com.android.internal.os.ZygoteConnection.runOnce(ZygoteConnection.java:242)
at com.android.internal。 os.ZygoteInit.runSelectLoop(ZygoteInit.java:713)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:649)
at dalvik.system.NativeStart.main(Native Method )


InfoWindowAdapter :

  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);
位图locationImage;

locationName.setText(marker.getTitle());
locationAddress.setText(marker.getSnippet());
网址imageURL;
尝试{
imageURL = new URL(markerInfo.get(marker.getId()));
locationImage = BitmapFactory.decodeStream(imageURL.openConnection()。getInputStream());
ivRow.setImageBitmap(locationImage);
} catch(Exception e){
e.printStackTrace();
}
返回viewGroup;
}

@Override
public View getInfoContents(Marker marker){return null;}
};

OnInfoWindowClickListener:

  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();
}
};

public final LocationListener locationListener = new LocationListener(){
@Override $ b $ public void onStatusChanged(String provider,int status,Bundle extras){}

@Override
public void onProviderEnabled(String provider){}
$ b @Override
public void onProviderDisabled(String provider){}

@Override
public void onLocationChanged(Location newLocation){

}
};

private LatLng myLatLng(){
LocationManager locationManager =(LocationManager)getSystemService(Context.LOCATION_SERVICE);
字符串提供程序;
位置位置;
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; (locationManager.getLastKnownLocation(provider)== null){
locationManager.requestLocationUpdates(provider,2000,10,locationListener);
}


if
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());
}
返回myLatLng == null? myLatLng():myLatLng;
}

感谢您提供任何解决方案。



更新:从加载的JSON创建标记。如果这可能导致此问题。

解决方案

这可能是一个线程问题。在从URL加载图像之前,会返回 viewGroup 。解决此问题的简单方法是使用第三方图像加载库 Picasso



示例代码:

  ImageView imageView =(ImageView)myContentsView.findViewById(R.id.ivRowImage); $(b)if(not_first_time_showing_info_window){
Picasso.with(getApplicationContext())。load(YOUR_IMG_URL)。(imageView);
} else {//如果这是第一次,使用回调集
加载图像not_first_time_showing_info_window = true;
Picasso.with(getApplicationContext())。load(YOUR_IMG_URL)。(imageView,new InfoWindowRefresher(marker));
}

然后你可以有一个专用的辅助方法来确保异步显示第一次点击:

  private class InfoWindowRefresher实现回调{
private Marker markerToRefresh;

私人InfoWindowRefresher(Marker markerToRefresh){
this.markerToRefresh = markerToRefresh;
}

@Override
public void onSuccess(){
markerToRefresh.showInfoWindow();

$ b @Override
public void onError(){}
}

你可以参考这个GitHub页面来获得完整的实现: https://github.com/jbj88817/GoogleMap-InfoWindow-android/blob/master/app/src/main/java/com/ bjiang / map_ex / MainActivity.java

如果你真的想用ImageLoader来做,你可以看看这篇文章: http://androidfreakers.blogspot.com/2013/08/display-custom-info- window-with.html
(但与使用Picasso从URL加载相比,它更复杂)


I have searched related issues but can't find it. I create an InfoWindowMarker which shows Picture, Name and Address. Then i create OnInfoWindowClickListener that will showing Latitude and Longitude of selected Marker. But when i click the Info Window i got this error message.

Logcat:

com.xxxxxxxx.xxxxxxxxxx E/Zygote﹕ Zygote: error closing descriptor libcore.io.ErrnoException: close failed: EBADF (Bad file number) at libcore.io.Posix.close(Native Method) at libcore.io.BlockGuardOs.close(BlockGuardOs.java:75) at com.android.internal.os.ZygoteInit.closeServerSocket(ZygoteInit.java:221) at com.android.internal.os.ZygoteConnection.handleChildProc(ZygoteConnection.java:879) at com.android.internal.os.ZygoteConnection.runOnce(ZygoteConnection.java:242) at com.android.internal.os.ZygoteInit.runSelectLoop(ZygoteInit.java:713) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:649) at dalvik.system.NativeStart.main(Native Method)

InfoWindowAdapter:

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;}
};

OnInfoWindowClickListener:

 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;
}

Thanks for any solution.

Update: Markers created from loaded JSON. If it is could causing this issue.

解决方案

This might be a threading issue. Your viewGroup is returned before your image is load from the URL. An easy way to solve the problem is to use the third party image loading library Picasso.

Sample code:

 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));
 }

And then you can have a private helper method to make sure the asynchronous shown in the first click:

 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() {}
    }

You can refer to this GitHub page for complete implementation: https://github.com/jbj88817/GoogleMap-InfoWindow-android/blob/master/app/src/main/java/com/bjiang/map_ex/MainActivity.java

If you really want to do it with ImageLoader, you can check out this post: http://androidfreakers.blogspot.com/2013/08/display-custom-info-window-with.html (But it is more complicated compared to using Picasso to load from URL)

这篇关于Android - EBADF(坏文件编号)OnClickInfoMarker的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆