gpt4 book ai didi

java - 如何修复 Android Studio 中的 ChannelException?

转载 作者:行者123 更新时间:2023-12-02 05:29:11 25 4
gpt4 key购买 nike

我想在 Android 设备上测试 io.netty(库)。但是当我运行该应用程序时,它会抛出 io.netty.channel.ChannelException。

我的调试器不会停在我设置 Netty-Bootstrap 的行上,程序在...之前崩溃(我也不明白)。我更深入地研究了后台代码,似乎 Android 开发人员更改了 Constructer.newInstance() 方法,这似乎是我的问题的原因。但我不知道为什么会抛出该异常以及如何避免这种情况。

这是客户端的设置(问题是 NioServerSocket.class,无法实例化)

import io.netty.bootstrap.Bootstrap;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.codec.string.StringDecoder;
import io.netty.handler.codec.string.StringEncoder;

public class NettyBootstrap {


private MainActivity parent;

public NettyBootstrap(MainActivity main) {
parent = main;

EventLoopGroup workerGroup = new NioEventLoopGroup();

try {
new Bootstrap()
.group(workerGroup)
.channel(NioSocketChannel.class)
.handler(new ChannelInitializer<SocketChannel>() {

@Override
protected void initChannel(SocketChannel ch) throws Exception {
ch.pipeline()
.addLast(new StringEncoder())
.addLast(new StringDecoder())
.addLast(new OwnChannelHandler(parent));
}
}).connect("localhost", 26666).sync().channel().closeFuture().syncUninterruptibly();
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
workerGroup.shutdownGracefully();
}
}
}

这是我的错误日志的一部分:

Caused by: io.netty.channel.ChannelException: Unable to create Channel from class class io.netty.channel.socket.nio.NioSocketChannel
at io.netty.channel.ReflectiveChannelFactory.newChannel(ReflectiveChannelFactory.java:46)
at io.netty.bootstrap.AbstractBootstrap.initAndRegister(AbstractBootstrap.java:320)
at io.netty.bootstrap.Bootstrap.doResolveAndConnect(Bootstrap.java:163)
at io.netty.bootstrap.Bootstrap.connect(Bootstrap.java:145)
at io.netty.bootstrap.Bootstrap.connect(Bootstrap.java:126)
at com.pxif.connectiontest.nio.NettyBootstrap.<init>(NettyBootstrap.java:43)
at com.pxif.connectiontest.MainActivity.onCreate(MainActivity.java:24)

编辑

我的整个错误日志(我仍然遗漏了 Android zygote 的内容):

E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.pxif.connectiontest, PID: 5485
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.pxif.connectiontest/com.pxif.connectiontest.MainActivity}: io.netty.channel.ChannelException: Unable to create Channel from class class io.netty.channel.socket.nio.NioSocketChannel
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2817)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6541)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
Caused by: io.netty.channel.ChannelException: Unable to create Channel from class class io.netty.channel.socket.nio.NioSocketChannel
at io.netty.channel.ReflectiveChannelFactory.newChannel(ReflectiveChannelFactory.java:46)
at io.netty.bootstrap.AbstractBootstrap.initAndRegister(AbstractBootstrap.java:320)
at io.netty.bootstrap.Bootstrap.doResolveAndConnect(Bootstrap.java:163)
at io.netty.bootstrap.Bootstrap.connect(Bootstrap.java:145)
at io.netty.bootstrap.Bootstrap.connect(Bootstrap.java:126)
at com.pxif.connectiontest.nio.NettyBootstrap.<init>(NettyBootstrap.java:37)
at com.pxif.connectiontest.MainActivity.onCreate(MainActivity.java:24)
at android.app.Activity.performCreate(Activity.java:6975)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1213)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2770)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892) 
at android.app.ActivityThread.-wrap11(Unknown Source:0) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593) 
at android.os.Handler.dispatchMessage(Handler.java:105) 
at android.os.Looper.loop(Looper.java:164) 
at android.app.ActivityThread.main(ActivityThread.java:6541) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767) 
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Constructor.newInstance0(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:334)
at io.netty.channel.ReflectiveChannelFactory.newChannel(ReflectiveChannelFactory.java:44)
at io.netty.bootstrap.AbstractBootstrap.initAndRegister(AbstractBootstrap.java:320) 
at io.netty.bootstrap.Bootstrap.doResolveAndConnect(Bootstrap.java:163) 
at io.netty.bootstrap.Bootstrap.connect(Bootstrap.java:145) 
at io.netty.bootstrap.Bootstrap.connect(Bootstrap.java:126) 
at com.pxif.connectiontest.nio.NettyBootstrap.<init>(NettyBootstrap.java:37) 
at com.pxif.connectiontest.MainActivity.onCreate(MainActivity.java:24) 
at android.app.Activity.performCreate(Activity.java:6975) 
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1213) 
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2770) 
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892) 
at android.app.ActivityThread.-wrap11(Unknown Source:0) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593) 
at android.os.Handler.dispatchMessage(Handler.java:105) 
at android.os.Looper.loop(Looper.java:164) 
at android.app.ActivityThread.main(ActivityThread.java:6541) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767) 
Caused by: io.netty.channel.ChannelException: Failed to open a socket.
at io.netty.channel.socket.nio.NioSocketChannel.newSocket(NioSocketChannel.java:70)
at io.netty.channel.socket.nio.NioSocketChannel.<init>(NioSocketChannel.java:87)
at io.netty.channel.socket.nio.NioSocketChannel.<init>(NioSocketChannel.java:80)
at java.lang.reflect.Constructor.newInstance0(Native Method) 
at java.lang.reflect.Constructor.newInstance(Constructor.java:334) 
at io.netty.channel.ReflectiveChannelFactory.newChannel(ReflectiveChannelFactory.java:44) 
at io.netty.bootstrap.AbstractBootstrap.initAndRegister(AbstractBootstrap.java:320) 
at io.netty.bootstrap.Bootstrap.doResolveAndConnect(Bootstrap.java:163) 
at io.netty.bootstrap.Bootstrap.connect(Bootstrap.java:145) 
at io.netty.bootstrap.Bootstrap.connect(Bootstrap.java:126) 
at com.pxif.connectiontest.nio.NettyBootstrap.<init>(NettyBootstrap.java:37) 
at com.pxif.connectiontest.MainActivity.onCreate(MainActivity.java:24) 
at android.app.Activity.performCreate(Activity.java:6975) 
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1213) 
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2770) 
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892) 
at android.app.ActivityThread.-wrap11(Unknown Source:0) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593) 
at android.os.Handler.dispatchMessage(Handler.java:105) 
at android.os.Looper.loop(Looper.java:164) 
at android.app.ActivityThread.main(ActivityThread.java:6541) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767) 
Caused by: java.net.SocketException: Permission denied
at sun.nio.ch.Net.socket0(Native Method)
at sun.nio.ch.Net.socket(Net.java:420)
at sun.nio.ch.Net.socket(Net.java:413)
at sun.nio.ch.SocketChannelImpl.<init>(SocketChannelImpl.java:128)
at sun.nio.ch.SelectorProviderImpl.openSocketChannel(SelectorProviderImpl.java:60)
at io.netty.channel.socket.nio.NioSocketChannel.newSocket(NioSocketChannel.java:68)
at io.netty.channel.socket.nio.NioSocketChannel.<init>(NioSocketChannel.java:87) 
at io.netty.channel.socket.nio.NioSocketChannel.<init>(NioSocketChannel.java:80) 
at java.lang.reflect.Constructor.newInstance0(Native Method) 
at java.lang.reflect.Constructor.newInstance(Constructor.java:334) 
at io.netty.channel.ReflectiveChannelFactory.newChannel(ReflectiveChannelFactory.java:44) 
at io.netty.bootstrap.AbstractBootstrap.initAndRegister(AbstractBootstrap.java:320) 
at io.netty.bootstrap.Bootstrap.doResolveAndConnect(Bootstrap.java:163) 
at io.netty.bootstrap.Bootstrap.connect(Bootstrap.java:145) 
at io.netty.bootstrap.Bootstrap.connect(Bootstrap.java:126) 
at com.pxif.connectiontest.nio.NettyBootstrap.<init>(NettyBootstrap.java:37) 
at com.pxif.connectiontest.MainActivity.onCreate(MainActivity.java:24) 
at android.app.Activity.performCreate(Activity.java:6975) 
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1213) 
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2770) 
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892) 
at android.app.ActivityThread.-wrap11(Unknown Source:0) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593) 
at android.os.Handler.dispatchMessage(Handler.java:105) 
at android.os.Looper.loop(Looper.java:164) 
at android.app.ActivityThread.main(ActivityThread.java:6541) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767) 
I/zygote: Background concurrent copying GC freed 9292(1386KB) AllocSpace objects, 0(0B) LOS objects, 58% free, 1077KB/2MB, paused 35.531ms total 135.042ms
E/System: Uncaught exception thrown by finalizer
E/System: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.io.FileDescriptor.isSocket$()' on a null object reference
at sun.nio.ch.FileDispatcherImpl.preCloseImpl(FileDispatcherImpl.java:115)
at sun.nio.ch.SocketDispatcher.preClose(SocketDispatcher.java:66)
at sun.nio.ch.SocketChannelImpl.implCloseSelectableChannel(SocketChannelImpl.java:883)
at java.nio.channels.spi.AbstractSelectableChannel.implCloseChannel(AbstractSelectableChannel.java:234)
at java.nio.channels.spi.AbstractInterruptibleChannel.close(AbstractInterruptibleChannel.java:116)
at sun.nio.ch.SocketChannelImpl.finalize(SocketChannelImpl.java:937)
at java.lang.Daemons$FinalizerDaemon.doFinalize(Daemons.java:250)
at java.lang.Daemons$FinalizerDaemon.runInternal(Daemons.java:237)
at java.lang.Daemons$Daemon.run(Daemons.java:103)
at java.lang.Thread.run(Thread.java:764)

感谢您的帮助:D

最佳答案

我可以看到打开套接字时出现权限被拒绝的异常。

尝试添加

    <uses-permission android:name="android.permission.INTERNET"/>

到您的 list 。

关于java - 如何修复 Android Studio 中的 ChannelException?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56210069/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com