我正在尝试创建一个具有编辑文本视图和按钮的应用程序。此按钮将通过 TCP 连接将编辑文本中的任何内容发送到服务器。
我已经完成了编辑文本的发送,但是在我单击一次按钮后,应用程序崩溃了。我怎样才能把它放到一个循环中,以便我可以发送多条消息?这是我的来源
public class MainActivity extends Activity {
//Handler h;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final EditText eText = (EditText) findViewById(R.id.address);
final TextView tView = (TextView) findViewById(R.id.pagetext);
final Button button = (Button) findViewById(R.id.ButtonGo);
button.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
try {
Socket s = new Socket("192.168.0.117", 4447);
BufferedReader in = new BufferedReader(new InputStreamReader(s.getInputStream()));
final BufferedWriter out = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
//send output msg
String outMsg = ((EditText)findViewById(R.id.address)).getText().toString().trim();
out.write(outMsg);
out.flush();
Log.i("TcpClient", "sent: " + outMsg);
} catch (UnknownHostException e) {
tView.setText(e.toString());
Log.v("Tcp",e.toString());
} catch (IOException e) {
tView.setText(e.toString());
Log.v("Tcp",e.toString());
}catch (Exception e) {
tView.setText(e.toString());
}
}
});
}
}