0

我的 Netty 通信类在标准 Java 应用程序中运行良好,但是当我在 Android 应用程序中运行它们时,我得到了 NullPointerException。

我的问题是,是否有任何 Netty 专家可以指出解决这个问题的正确方向。这是与 Android 的不兼容问题,还是您在堆栈跟踪中看到任何可能表明我可能做错了什么的东西?

我在用netty-3.4.0.Final.jar

我已将 Internet 权限添加到AndroidManifest.xml.
我已经验证我可以在 android 应用程序中仅使用原始套接字(没有 Netty)成功连接到服务器。

调用后,bootstrap.connect()我可以在两个线程的断点处暂停执行。一进exceptionCaught()一进channelClosed()

下面的堆栈跟踪来自异常exceptionCaught()

该项目的完整源代码codeplex位于elvemobileandroid project.

我相信我实际上看到过一次 Android 项目工作,这可能表明异常并不总是被抛出。不幸的是,我无法让它再次工作(即使我没有对项目进行任何更改)。

Thread [<1> main] (Suspended (breakpoint at line 172 in UptimeClientHandler))   
    UptimeClientHandler.exceptionCaught(ChannelHandlerContext, ExceptionEvent) line: 172    
    UptimeClientHandler(SimpleChannelUpstreamHandler).handleUpstream(ChannelHandlerContext, ChannelEvent) line: 117 
    DefaultChannelPipeline.sendUpstream(DefaultChannelPipeline$DefaultChannelHandlerContext, ChannelEvent) line: 564    
    DefaultChannelPipeline$DefaultChannelHandlerContext.sendUpstream(ChannelEvent) line: 792    
    ReadTimeoutHandler(SimpleChannelUpstreamHandler).exceptionCaught(ChannelHandlerContext, ExceptionEvent) line: 143   
    ReadTimeoutHandler(SimpleChannelUpstreamHandler).handleUpstream(ChannelHandlerContext, ChannelEvent) line: 117  
    DefaultChannelPipeline.sendUpstream(DefaultChannelPipeline$DefaultChannelHandlerContext, ChannelEvent) line: 564    
    DefaultChannelPipeline.sendUpstream(ChannelEvent) line: 559 
    Channels.fireExceptionCaught(Channel, Throwable) line: 533  
    NioClientSocketPipelineSink.connect(NioClientSocketChannel, ChannelFuture, SocketAddress) line: 152 
    NioClientSocketPipelineSink.eventSunk(ChannelPipeline, ChannelEvent) line: 97   
    DefaultChannelPipeline.sendDownstream(ChannelEvent) line: 574   
    Channels.connect(Channel, SocketAddress) line: 642  
    NioClientSocketChannel(AbstractChannel).connect(SocketAddress) line: 204    
    ClientBootstrap.connect(SocketAddress, SocketAddress) line: 230 
    ClientBootstrap.connect(SocketAddress) line: 183    
    ClientBootstrap.connect() line: 154 
    UptimeClient.run() line: 94 
    CommunicationTest.test() line: 32   
    ElveMobileActivity.onCreate(Bundle) line: 25    
    Instrumentation.callActivityOnCreate(Activity, Bundle) line: 1047   
    ActivityThread.performLaunchActivity(ActivityThread$ActivityRecord, Intent) line: 2627  
    ActivityThread.handleLaunchActivity(ActivityThread$ActivityRecord, Intent) line: 2679   
    ActivityThread.access$2300(ActivityThread, ActivityThread$ActivityRecord, Intent) line: 125 
    ActivityThread$H.handleMessage(Message) line: 2033  
    ActivityThread$H(Handler).dispatchMessage(Message) line: 99 
    Looper.loop() line: 123 
    ActivityThread.main(String[]) line: 4627    
    Method.invokeNative(Object, Object[], Class, Class[], Class, int, boolean) line: not available [native method]  
    Method.invoke(Object, Object...) line: 521  
    ZygoteInit$MethodAndArgsCaller.run() line: 868  
    ZygoteInit.main(String[]) line: 626 
    NativeStart.main(String[]) line: not available [native method]  

这是我在stackoverflow上的第一篇文章,如果我可以提供更多信息,请告诉我。

谢谢

4

1 回答 1

1

感谢您指出 logcat 窗口(我是 Eclipse 新手)。它显示了另一个错误:java.net.SocketException: Bad address family

在做了一些研究后,我发现了这个: http ://code.google.com/p/android/issues/detail?id=9431

修复是在使用模拟器时禁用 IPv6:

java.lang.System.setProperty("java.net.preferIPv6Addresses", "false");
于 2012-04-24T07:48:26.150 回答