为啥当我尝试检查套接字连接状态时应用程序崩溃?
Posted
技术标签:
【中文标题】为啥当我尝试检查套接字连接状态时应用程序崩溃?【英文标题】:Why App ceashes when I try to check socket connection status?为什么当我尝试检查套接字连接状态时应用程序崩溃? 【发布时间】:2014-12-09 07:00:12 【问题描述】:我正在构建远程 PC 控制应用程序。 我的代码 我只想在套接字连接处于活动状态或通知用户它不活动时才开始新活动
class MyActivity extends Activity
static Boolean connect;
Socket client;
SendMessage send;
//other codes
bSend.setOnclickListener (new View.OnClikcListener ()
send=new SendMessage ();
send.execute ();
if (connect==true)
Intent I=new Intent (MyActivity.this, SubActivity.class);
startActivity (I);
else
tStatus.setText ("Failed");
);
class Send messages extends AsyncTask < >
protected void doInBackground (.....)
client=new Socket("192.168.237.1", 6666);
if(client.isConnected==true)
connect=true;
else
connect=false;
但是当我点击按钮时,应用程序崩溃了 代码中的实际问题在哪里??
这是我的日志
12-08 21:13:47.930: E SensorManager(11323): thread start
12-08 21:13:47.935: D SensorManager(11323) registerListener :: handle = 0 name= LSM330DLC 3-axis Accelerometer delay= 200000 Listener= android.view.OrientationEventListener$SensorEventListenerImpl@41db1a58
12-08 21:13:47.945: D/SensorManager(11323): unregisterListener:: Listener= android.view.OrientationEventListener$SensorEventListenerImpl@41db1a58
12-08 21:13:47.945: D/Sensors(11323): Remain listener = Sending .. normal delay 200ms
12-08 21:13:47.945: I/Sensors(11323): sendDelay --- 200000000
12-08 21:13:47.945: D/SensorManager(11323): JNI - sendDelay
12-08 21:13:47.945: I/SensorManager(11323): Set normal delay = true
12-08 21:13:47.985: D/SensorManager(11323): registerListener :: handle = 0 name= LSM330DLC 3-axis Accelerometer delay= 200000 Listener= android.view.OrientationEventListener$SensorEventListenerImpl@41db1a58
12-08 21:13:48.030: D/libEGL(11323): loaded /system/lib/egl/libEGL_mali.so
12-08 21:13:48.040: D/libEGL(11323): loaded /system/lib/egl/libGLESv1_CM_mali.so
12-08 21:13:48.045: D/libEGL(11323): loaded /system/lib/egl/libGLESv2_mali.so
12-08 21:13:48.050: D/(11323): Device driver API match
12-08 21:13:48.050: D/(11323): Device driver API version: 10
12-08 21:13:48.050: D/(11323): User space API version: 10
12-08 21:13:48.050: D/(11323): mali: REVISION=Linux-r2p4-02rel0 BUILD_DATE=Tue Oct 16 15:37:13 KST 2012
12-08 21:13:48.090: D/OpenGLRenderer(11323): Enabling debug mode 0
12-08 21:13:50.020: D/AndroidRuntime(11323): Shutting down VM
12-08 21:13:50.020: W/dalvikvm(11323): threadid=1: thread exiting with uncaught exception (group=0x411302a0)
12-08 21:13:50.040: E/AndroidRuntime(11323): FATAL EXCEPTION: main
12-08 21:13:50.040: E/AndroidRuntime(11323): java.lang.NullPointerException
12-08 21:13:50.040: E/AndroidRuntime(11323): at com.example.workarea.MainActivity$1.onClick(MainActivity.java:49)
12-08 21:13:50.040: E/AndroidRuntime(11323): at android.view.View.performClick(View.java:4211)
12-08 21:13:50.040: E/AndroidRuntime(11323): at android.view.View$PerformClick.run(View.java:17267)
12-08 21:13:50.040: E/AndroidRuntime(11323): at android.os.Handler.handleCallback(Handler.java:615)
12-08 21:13:50.040: E/AndroidRuntime(11323): at android.os.Handler.dispatchMessage(Handler.java:92)
12-08 21:13:50.040: E/AndroidRuntime(11323): at android.os.Looper.loop(Looper.java:137)
12-08 21:13:50.040: E/AndroidRuntime(11323): at android.app.ActivityThread.main(ActivityThread.java:4898)
12-08 21:13:50.040: E/AndroidRuntime(11323): at java.lang.reflect.Method.invokeNative(Native Method)
12-08 21:13:50.040: E/AndroidRuntime(11323): at java.lang.reflect.Method.invoke(Method.java:511)
12-08 21:13:50.040: E/AndroidRuntime(11323): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1006)
12-08 21:13:50.040: E/AndroidRuntime(11323): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773)
12-08 21:13:50.040: E/AndroidRuntime(11323): at dalvik.system.NativeStart.main(Native Method)
12-08 21:14:07.425: I/Process(11323): Sending signal. PID: 11323 SIG: 9
【问题讨论】:
【参考方案1】:if (connect==true)
Intent I=new Intent (MyActivity.this, SubActivity.class);
startActivity (I);
else
tStatus.setText ("Failed");
您必须在异步任务的 onPostExecute 方法中添加此代码。
【讨论】:
谢谢,现在它工作了!!!当移动到另一个活动(除了android服务)时,有什么方法可以保持套接字连接吗? 兄弟!当 connect==false 时,它不会在文本框中显示“失败” 静态布尔连接=false;更改此声明。【参考方案2】:你在哪里执行异步任务?看起来 connect 的值仅根据异步任务的执行进行初始化。
【讨论】:
已编辑帖子...请重新检查以上是关于为啥当我尝试检查套接字连接状态时应用程序崩溃?的主要内容,如果未能解决你的问题,请参考以下文章
如何在 VB.NET 中检查 TCP 服务器(套接字)与 TCP 客户端的连接状态
当我使用 http1.1 协议时,为啥 style.css 文件不在同一个 TCP 连接中?