Note that there are some explanatory texts on larger screens.

plurals
  1. POProblem when I try to move socket creation code to another activity?
    primarykey
    data
    text
    <p>I have an android application created by eclipse. This application works as a client connecting to a server wirelessly across a socket connection. I have a problem with the code of socket creation. When I try to move the code from an activity to another in my application, then it doesn't work.</p> <p>Here is the first activity, where the code works:</p> <pre><code>public class MyNewAndroidActivity extends Activity { protected Socket socket; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); final Button button_connect = (Button) findViewById(R.id.button1); final Button button_disconnect = (Button) findViewById(R.id.button2); final Button b_forward = (Button) findViewById(R.id.b_forward); final Button b_backward = (Button) findViewById(R.id.b_backward); final Button b_right = (Button) findViewById(R.id.b_right); final Button b_left = (Button) findViewById(R.id.b_left); final Button b_stop = (Button) findViewById(R.id.b_stop); button_connect.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { try { // Create socket with time out //192.168.1.76 // my code which I want to transfer it. int port = 2005; SocketAddress sockaddr = new InetSocketAddress("192.168.1.15", port); socket = new Socket(); int timeoutMs = 2000; socket.connect(sockaddr, timeoutMs); } catch (UnknownHostException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (SocketException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (SecurityException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }); </code></pre> <p>when I convert it to method in another activity then it doesn't work:</p> <pre><code>public class IBMEyes extends Activity implements SensorListener{ protected static Socket socket; final String tag = "MyProject"; String state ="off"; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); sm = (SensorManager) getSystemService(SENSOR_SERVICE); setContentView(R.layout.main); Button b_connect = (Button) findViewById(R.id.button1); b_connect.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub SocketConnect(); } }); // here is my code that I've transferred it. public void SocketConnect() { // TODO Auto-generated method stub try { int port = 2005; SocketAddress sockaddr = new InetSocketAddress("192.168.1.15", port); socket = new Socket(); int timeoutMs = 2000; socket.connect(sockaddr, timeoutMs); } catch (UnknownHostException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (SocketException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (SecurityException e) { // TODO Auto-generated catch block e.printStackTrace(); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload