为什么一个Socket不是的instanceof可关闭在运行时? [英] why a Socket is not instanceof Closeable at runtime?

查看:114
本文介绍了为什么一个Socket不是的instanceof可关闭在运行时?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在一个Android应用程序,我说这code到的的onCreate()

In an Android app, I added this code to onCreate()

    Closeable sss = new Socket();
    if (!(sss instanceof Closeable)) {
        throw new RuntimeException("Something unexpected happened");
    }

的进口是:

import java.io.Closeable;
import java.net.Socket;

在code编译,但 我得到异常:

The code compiles, but I am getting the exception:

E/AndroidRuntime( 8293): java.lang.RuntimeException: Unable to start activity...: java.lang.RuntimeException: Something unexpected happened
...
E/AndroidRuntime( 8293): Caused by: java.lang.RuntimeException: Something unexpected happened
...

在不同的情况下,故障导致 java.lang.ArrayStoreException (即 java.lang.ArrayStoreException:java.net.Socket中不能存储在类型的数组java.io.Closeable [] 的)。

In a different context, the glitch causes a java.lang.ArrayStoreException (namely, java.lang.ArrayStoreException: java.net.Socket cannot be stored in an array of type java.io.Closeable[]).

我缺少的东西? 任何想法怎么办?

Am I missing something? Any idea what to do?

修改 同样,当的DatagramSocket 作为 MyClass的< D​​atagramSocket的> MyClass的<吨延伸可关闭> ,它引起的 java.lang.IncompatibleClassChangeError:接口没有实现

EDIT Similarly, when DatagramSocket is used as MyClass<DatagramSocket> for MyClass<T extends Closeable>, it causes java.lang.IncompatibleClassChangeError: interface not implemented.

推荐答案

出现在Android API等级这个问题之前19.在受影响的版本,插座没有实现可关闭的。

This issue occurs on Android API levels prior to 19. On affected versions, Socket does not implement Closeable.

来源:

  • Android: Exception using Closeable interface with Socket
  • https://code.google.com/p/android/issues/detail?id=62909

这篇关于为什么一个Socket不是的instanceof可关闭在运行时?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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