Android - EBADF(错误文件号)OnClickInfoMarker

2024-03-05

我搜索过相关问题但没有找到。我创建了一个显示图片、名称和地址的 InfoWindowMarker。然后我创建 OnInfoWindowClickListener 将显示所选标记的纬度和经度。但是当我单击信息窗口时,我收到此错误消息。

Logcat:

com.xxxxxxxx.xxxxxxxxx E/Zygote:Zygote:关闭描述符时出错 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(本机方法)

信息窗口适配器:

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

感谢任何解决方案。

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


这可能是线程问题。你的viewGroup在从 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 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页面:https://github.com/jbj88817/GoogleMap-InfoWindow-android/blob/master/app/src/main/java/com/bjian/map_ex/MainActivity.java 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 http://androidfreakers.blogspot.com/2013/08/display-custom-info-window-with.html(但是比使用Picasso从URL加载要复杂一些)

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Android - EBADF(错误文件号)OnClickInfoMarker 的相关文章

随机推荐

  • 如何在 openLayer 地图中加载本地 gpx 文件?

    我认为标题很清楚 我正在使用 openLayer 库 v4 6 5 并且我试图在加载页面时在地图中加载本地 GPX 文件 在官方文档中 在 GPX 数据示例中 https openlayers org en latest examples
  • TSLint 错误:: 节点解释器路径不正确。请检查口译员设置

    我是 Angular 的新手 很想知道错误是什么 如何解决呢 我正在使用网络风暴 IDE 这就是我在 Intellij 中摆脱这个警告的方法 改变这个 to this
  • Sagemaker:如何在 Predictor 中设置 content_type(Sagemake > 2.0)?

    请求帮助解决以下错误 调用 InvokeEndpoint 时发生错误 ModelError 操作 从模型收到客户端错误 415 和消息 不支持内容类型应用程序 八位字节流 支持 内容类型是文本 csv 文本 libsvm 这是相关代码 fr
  • 箭头函数“预期表达式”语法错误

    我想改造这段代码 var formatQuoteAmount function tx return Currency toSmallestSubunit tx usd USD var quoteAmounts res transaction
  • 如何在列表上触发 Traits 静态事件通知?

    我正在通过traits https github com enthought traits PyCon 2010 的演示 http python mirocommunity org video 1690 pycon 2010 introdu
  • 将数据帧写入 csv 文件,其中 NA 值为空白

    有一个dataframe named cnbd 例如 cnbd data frame 1 2 3 NA NA 5 因此表达式 dim cnbd 1 give 1 我想写一个像这样的数据框cnbd到 csv write file filena
  • React 路由器:在每个 导航上执行自定义函数

    抱歉 如果这个问题已经得到回答 但是有没有一种方法可以在每个导航 最好不创建自定义包装纸 我想在应用程序内的每次导航之前将一些信息放入 sessionStorage 中 Thanks 您可以使用onClick执行任何操作 例如 gt con
  • Django 1.3 中链接静态文件的问题

    我在 Windows XP 上运行 django 1 3 python 2 7 我正在尝试在我的 django 应用程序的静态文件夹中设置 css 模板如下所示 生成的 HTML 如下所示
  • 正则表达式排除 [ 除非前面有 \

    如何编写一个正则表达式来接受包含任意数量的除 之外的任何字符的表达式 除非 前面是 例子 this is text this also this isn t any more 从上面的文字来看 this is text this also
  • @Html.DropDownList 宽度

    问 如何设置 Html DropDownList 的宽度 而不是在 css 中 Html DropDownList ListId String Empty new style width 250px no go 该的第二个论点下拉列表 ht
  • 与有向边的最大加权二分匹配

    我知道计算最大加权匹配的各种算法加权 无向二分图 即分配问题 例如 匈牙利算法 贝尔曼 福特算法甚至 Blossom 算法 适用于一般图 即非二分图 但是 如果二分图的边是 如何计算最大加权匹配加权和定向 我希望能够提供具有多项式复杂度的算
  • java中的equals方法

    我读过有关equals java中的方法 我听说它只是根据价值进行比较 但是为什么在下面的情况下它返回 false 其中值相同但类型不同 public class test public static void main String ar
  • Coq :> 符号

    这可能是非常微不足道的 但我找不到任何关于 gt 符号在 Coq 中含义的信息 有什么区别 U 类型 和 W gt 类型 这取决于符号出现的位置 例如 如果它位于记录声明内 它会指示 Coq 添加相应的记录投影作为强制 具体来说 假设我们有
  • 如何在打字稿中使用不同类型的通用键输入对象

    您将如何在打字稿中输入这些对象 我有一个特殊的 日期时间 键 即日期 其余键是数字 但我事先并不知道每个对象上将设置哪些键 示例值 type Metrics const example1 Metrics datetime new Date
  • Facebook FB.login 在 Safari 中有效,但在移动 Safari 中无效

    以下 FB Login 函数在桌面 Chrome FF 和 Safari 中运行良好 但在移动 Safari 中 在 iPhone 4S 上测试 它挂起并且不会返回 FB login 回调 当我使用 Safari 并将用户代理设置为 Saf
  • 检查是否有节点但没有文本

    我有以下 xml 片段 Case1
  • CSS菜单IE7问题

    菜单适用于所有主要浏览器 但在 IE7 中看起来有所不同 请参阅下面的屏幕截图 查看演示 http jsfiddle net FQLdm 6 http jsfiddle net FQLdm 6 当您将鼠标悬停在主页链接上时 您将看到一个子菜
  • 如何在 intelliJ-idea 中最好地调试 solr?

    如何在 intelliJ idea 中最好地调试 solr 哪条路容易 当向 solr 发布查询时 我想处理这个问题 您可以设置远程调试 在里面运行 调试配置 然后当使用默认的jetty 这个处于多核模式 启动solr时 传入 java a
  • 按第一个字母对字符串数组进行分组

    我面临这个挑战 其中包括 编写一个以字符串数组作为参数的函数 然后 按第一个字母对数组中的字符串进行分组 返回一个对象 其中包含带有代表第一个字母的键的属性 例如 应该返回 groupIt hola adios chao hemos acc
  • Android - EBADF(错误文件号)OnClickInfoMarker

    我搜索过相关问题但没有找到 我创建了一个显示图片 名称和地址的 InfoWindowMarker 然后我创建 OnInfoWindowClickListener 将显示所选标记的纬度和经度 但是当我单击信息窗口时 我收到此错误消息 Logc