I have a problem in the socket of the following code which made the program when I run it on AVD to stopped working (Unfortunately your -app- has stopped), I use Android 4.0 platform on Windows 7 ..
I tried to move the socket section to the button click, so when I click on the button the program stopped working, so here in the socket definition make the error. (Socket socket;)
public class ServerClient extends Activity {
// declaration of button, textView
private Button bt;
private TextView tv;
//port number
private static final int REDIRECTED_SERVERPORT = 5000;
//ip address
private String serverIpAddress = "10.0.2.2";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
bt = (Button) findViewById(R.id.myButton);
tv = (TextView) findViewById(R.id.myTextView);
// on click on the button the socket will be created
bt.setOnClickListener(new OnClickListener() {
Socket socket; //this line cause the app to stop working
public void onClick(View v) {
try {
InetAddress serverAddr = InetAddress.getByName(serverIpAddress);
socket = new Socket(serverAddr, REDIRECTED_SERVERPORT);
} catch (UnknownHostException e1) {
e1.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
}
try {
EditText et = (EditText) findViewById(R.id.EditText01);
String str = et.getText().toString();
PrintWriter out = new PrintWriter(new BufferedWriter(
new OutputStreamWriter(socket.getOutputStream())),
true);
out.println(str);
Log.d("Client", "Client sent message");
} catch (UnknownHostException e) {
tv.setText("Error1");
e.printStackTrace();
} catch (IOException e) {
tv.setText("Error2");
e.printStackTrace();
} catch (Exception e) {
tv.setText("Error3");
e.printStackTrace();
}
}
});
}
}
Hello I had the same problem (Windows 7, Android 4.03). I have solved the problem using android 2.33 (API level 10) and emulator with android 2.33 kernel.