[英]ServerSocket.accept() terminates the Android Application
guys. 家伙。 I'm new to Android and the forum.
我是Android和论坛的新手。 I have the following problem which bothers me about socket communication.
我有以下困扰我套接字通信的问题。
package com.example.d5server2;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity {
TextView textview1;
Button button1;
ServerSocket serversocket1 = null;
Socket socket1 = null;
int i=16;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textview1 = (TextView) findViewById(R.id.textView1);
button1 = (Button) findViewById(R.id.button1);
}
@Override
protected void onResume(){
super.onResume();
try {
serversocket1 = new ServerSocket(9999);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
socket1 = serversocket1.accept();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
When I run this code on my phone the application gets terminated. 当我在手机上运行此代码时,应用程序将终止。 Yet if I delete the line
但是如果我删除该行
socket1 = serversocket1.accept();
it doesn't do so. 它没有这样做。 I checked many sample codes and most of them used a similar coding.
我检查了许多示例代码,其中大多数使用了类似的代码。 I can't find any reasons why my app gets terminated.
我找不到我的应用被终止的任何原因。 I search the web but couldn't get any answer for this.
我在网上搜索,但无法获得任何答案。 Thanks for all your help.
感谢你的帮助。
可能是因为您没有提供足够的信息(例如堆栈跟踪),所以'serverSocket'可能为null,因为错误的异常处理使应用程序即使ServerSocket的初始化引发异常也可以继续。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.