我的应用程序使用套接字与 PLC 通信。我在 onCreate() 中为每个 PLC 创建一个 Socket 并连接它们。
然后我启动两个线程(因为我有两种类型的 PLC)并开始通信。该应用程序运行良好,我可以交流。
关键是我想在我的应用程序不在前台时停止通信。
@Override
public void onStop()
{
super.onStop();
thread_imo.Pause();
thread_modbus.Pause();
for(cPeriphIMO autom : automates)
{
if(autom.isConnected())
autom.Deconnexion();
}
}
autom 得到了 Socket,而 autom.Deconnexion 只是关闭了这个 Socket。
但是当涉及到重新启动通信时,我遇到了问题。
@Override
public void onRestart()
{
super.onRestart();
for(cPeriphIMO autom : automates)
{
autom.Connexion();
}
thread_imo.Resume();
thread_modbus.Resume();
}
它应该重新连接套接字,我认为这很容易。
public boolean Connexion()
{
try
{
SocketAddress adr = new InetSocketAddress(ip, Integer.parseInt(port));
//THIS LINE CAUSES ERROR
socket.connect(adr, 1000);
}
catch(Exception e) {socket = null; Log.i("test", "error");}
if (socket != null)
{
try
{
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
out = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
} catch (Exception e) {return false;}
return true;
}
else {return false;}
}
但是当我尝试连接我的套接字时,应用程序崩溃了,它甚至没有捕捉到错误,我无法弄清楚。即使套接字不为空,我也会得到 NullPointerException :
07-08 15:51:46.330: E/AndroidRuntime(29498): FATAL EXCEPTION: main
07-08 15:51:46.330: E/AndroidRuntime(29498): java.lang.RuntimeException: Unable to resume activity {com.technicachat.webdatadomo/com.technicachat.webdatadomo.CIHM}: android.os.NetworkOnMainThreadException
07-08 15:51:46.330: E/AndroidRuntime(29498): at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2616)
07-08 15:51:46.330: E/AndroidRuntime(29498): at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2644)
07-08 15:51:46.330: E/AndroidRuntime(29498): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1269)
07-08 15:51:46.330: E/AndroidRuntime(29498): at android.os.Handler.dispatchMessage(Handler.java:99)
07-08 15:51:46.330: E/AndroidRuntime(29498): at android.os.Looper.loop(Looper.java:137)
07-08 15:51:46.330: E/AndroidRuntime(29498): at android.app.ActivityThread.main(ActivityThread.java:4898)
07-08 15:51:46.330: E/AndroidRuntime(29498): at java.lang.reflect.Method.invokeNative(Native Method)
07-08 15:51:46.330: E/AndroidRuntime(29498): at java.lang.reflect.Method.invoke(Method.java:511)
07-08 15:51:46.330: E/AndroidRuntime(29498): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1006)
07-08 15:51:46.330: E/AndroidRuntime(29498): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773)
07-08 15:51:46.330: E/AndroidRuntime(29498): at dalvik.system.NativeStart.main(Native Method)
07-08 15:51:46.330: E/AndroidRuntime(29498): Caused by: android.os.NetworkOnMainThreadException
07-08 15:51:46.330: E/AndroidRuntime(29498): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1118)
07-08 15:51:46.330: E/AndroidRuntime(29498): at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:84)
07-08 15:51:46.330: E/AndroidRuntime(29498): at libcore.io.IoBridge.connectErrno(IoBridge.java:144)
07-08 15:51:46.330: E/AndroidRuntime(29498): at libcore.io.IoBridge.connect(IoBridge.java:112)
07-08 15:51:46.330: E/AndroidRuntime(29498): at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:192)
07-08 15:51:46.330: E/AndroidRuntime(29498): at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:459)
07-08 15:51:46.330: E/AndroidRuntime(29498): at java.net.Socket.connect(Socket.java:842)
07-08 15:51:46.330: E/AndroidRuntime(29498): at com.technicachat.webdatadomo.IMO.CIMO.Connexion(CIMO.java:52)
07-08 15:51:46.330: E/AndroidRuntime(29498): at com.technicachat.webdatadomo.variables.cPeriphIMO.Connexion(cPeriphIMO.java:40)
07-08 15:51:46.330: E/AndroidRuntime(29498): at com.technicachat.webdatadomo.CIHM.onRestart(CIHM.java:591)
07-08 15:51:46.330: E/AndroidRuntime(29498): at android.app.Instrumentation.callActivityOnRestart(Instrumentation.java:1177)
07-08 15:51:46.330: E/AndroidRuntime(29498): at android.app.Activity.performRestart(Activity.java:5261)
07-08 15:51:46.330: E/AndroidRuntime(29498): at android.app.Activity.performResume(Activity.java:5272)
07-08 15:51:46.330: E/AndroidRuntime(29498): at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2606)
07-08 15:51:46.330: E/AndroidRuntime(29498): ... 10 more
我尝试自己调试它,阅读了该站点上的大量修复程序,但我似乎无法理解发生了什么。
多谢你们