为什么一个Socket不是的instanceof可关闭在运行时? [英] why a Socket is not instanceof Closeable at runtime?
问题描述
在一个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的< DatagramSocket的>
为 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.
来源:
- <一个href="http://stackoverflow.com/questions/21353474/android-exception-using-closeable-interface-with-socket">Android:除了使用可关闭的接口插槽
- HTTPS://$c$c.google。 COM / P /安卓/问题/详细信息?ID = 62909
- Android: Exception using Closeable interface with Socket
- https://code.google.com/p/android/issues/detail?id=62909
这篇关于为什么一个Socket不是的instanceof可关闭在运行时?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!