title: Application Force Closes On Startup
body: <p>Well, I am trying to make an app that communicates with a socket I have set up, tested, and made work in the background of a simple java application.</p>
  
  <p>The problem is, my app keeps force closing right away. You don't get a chance to see anything before it fcs.</p>
  
  <p>Here is my code:</p>
  
  <pre><code>import java.io.IOException;
  import java.io.PrintWriter;
  import java.net.InetAddress;
  import java.net.Socket;
  import java.net.UnknownHostException;
  import java.util.ArrayList;
  import java.util.Scanner;
  
  import android.app.Activity;
  import android.os.Bundle;
  import android.view.View;
  import android.view.View.OnClickListener;
  import android.widget.EditText;
  import android.widget.Toast;
  
  public class LogMeInClientActivity extends Activity {
      private Socket s;
      private InetAddress hostIp, localIp;
      private final String hostName = "localhost";
      private final int port = 4000;
      private Scanner in;
      private PrintWriter out;
      private final EditText usernameField = (EditText)     findViewById(R.id.username_field),
              passwordField = (EditText) findViewById(R.id.password_field);
      private String username, password;
  
      @Override
      public void onCreate(Bundle savedInstanceState) {
          System.out.println("A");
          super.onCreate(savedInstanceState);
          setContentView(R.layout.main);
          System.out.println("B");
          try {
              hostIp = InetAddress.getByName(hostName);
              localIp = InetAddress.getLocalHost();
              System.out.println("Local Ip: " + localIp.toString());
              System.out.println("Host Ip: " + hostIp.toString());
          } catch (UnknownHostException e) {
              e.printStackTrace();
          }
          try {
              s = new Socket(hostIp, port);
              out = new PrintWriter(s.getOutputStream(), true);
              in = new Scanner(s.getInputStream());
          } catch (IOException e) {
              e.printStackTrace();
          }
      }
  
      private class SignInButtonListener implements OnClickListener {
  
          public void onClick(View clickedButton) {
              switch (clickedButton.getId()) {
              case R.id.sign_in:
                  username = usernameField.getText().toString();
                  password = passwordField.getText().toString();
                  if (username.length() != 0 &amp;&amp; password.length() != 0) {
                      out.println(username);
                      out.println(password);
                  }
                  out.flush();
                  break;
  
              default:
                  break;
              }
          }
      }
  }
  </code></pre>
  
  <p>In the above code, neither "A" or "B" prints out. The application simply fc's.</p>
  
  <p>Here is my manifest:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.chriswins2much.Util"
      android:versionCode="1"
      android:versionName="1.0" &gt;
  
      &lt;uses-sdk android:minSdkVersion="15" /&gt;
      &lt;uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /&gt;
  
      &lt;application
          android:icon="@drawable/ic_launcher"
          android:label="@string/app_name" &gt;
          &lt;activity
              android:name=".LogMeInClientActivity"
              android:label="@string/app_name" &gt;
              &lt;intent-filter&gt;
                  &lt;action android:name="android.intent.action.MAIN" /&gt;
  
                  &lt;category android:name="android.intent.category.LAUNCHER" /&gt;
              &lt;/intent-filter&gt;
          &lt;/activity&gt;
      &lt;/application&gt;
  
  &lt;/manifest&gt;
  </code></pre>
  
  <p>I used the above permission, because I heard it would help for it.</p>
  
  <p>I updated the code to fix for the EditText NullPointerException fix (Only changed parts):</p>
  
  <pre><code>private EditText usernameField, passwordField;
  public void onCreate(Bundle savedInstanceState) {
          System.out.println("A");
          super.onCreate(savedInstanceState);
          setContentView(R.layout.main);
          usernameField = (EditText) findViewById(R.id.username_field);
          passwordField = (EditText) findViewById(R.id.password_field);
          System.out.println("B");
          try {
                      // It's saying one of the errors are on line 37 (directly below)
              hostIp = InetAddress.getByName(hostName);
              localIp = InetAddress.getLocalHost();
              System.out.println("Local Ip: " + localIp.toString());
              System.out.println("Host Ip: " + hostIp.toString());
          } catch (UnknownHostException e) {
              e.printStackTrace();
          }
          try {
              s = new Socket(hostIp, port);
              out = new PrintWriter(s.getOutputStream(), true);
              in = new Scanner(s.getInputStream());
          } catch (IOException e) {
              e.printStackTrace();
          }
      }
  </code></pre>
  
  <p>New logcat below:</p>
  
  <pre><code>04-19 02:46:56.292: I/dalvikvm(1289): threadid=3: reacting to signal 3
  04-19 02:46:57.049: I/dalvikvm(1289): Wrote stack traces to '/data/anr/traces.txt'
  04-19 02:46:57.103: I/dalvikvm(1289): threadid=3: reacting to signal 3
  04-19 02:46:57.450: E/dalvikvm(1289): Unable to open stack trace file '/data/anr/traces.txt': Permission denied
  04-19 02:46:57.462: I/dalvikvm(1289): threadid=3: reacting to signal 3
  04-19 02:46:57.669: I/dalvikvm(1289): Wrote stack traces to '/data/anr/traces.txt'
  04-19 02:46:58.069: I/dalvikvm(1289): threadid=3: reacting to signal 3
  04-19 02:46:58.409: I/dalvikvm(1289): Wrote stack traces to '/data/anr/traces.txt'
  04-19 02:46:58.650: I/dalvikvm(1289): threadid=3: reacting to signal 3
  04-19 02:46:58.779: I/dalvikvm(1289): Wrote stack traces to '/data/anr/traces.txt'
  04-19 02:46:59.009: I/System.out(1289): A
  04-19 02:46:59.153: I/dalvikvm(1289): threadid=3: reacting to signal 3
  04-19 02:46:59.321: I/dalvikvm(1289): Wrote stack traces to '/data/anr/traces.txt'
  04-19 02:46:59.639: I/dalvikvm(1289): threadid=3: reacting to signal 3
  04-19 02:46:59.879: I/dalvikvm(1289): Wrote stack traces to '/data/anr/traces.txt'
  04-19 02:47:00.249: I/dalvikvm(1289): threadid=3: reacting to signal 3
  04-19 02:47:00.490: I/dalvikvm(1289): Wrote stack traces to '/data/anr/traces.txt'
  04-19 02:47:00.819: I/dalvikvm(1289): threadid=3: reacting to signal 3
  04-19 02:47:00.970: I/dalvikvm(1289): Wrote stack traces to '/data/anr/traces.txt'
  04-19 02:47:01.279: I/dalvikvm(1289): threadid=3: reacting to signal 3
  04-19 02:47:01.477: I/dalvikvm(1289): Wrote stack traces to '/data/anr/traces.txt'
  04-19 02:47:01.791: I/dalvikvm(1289): threadid=3: reacting to signal 3
  04-19 02:47:01.919: I/dalvikvm(1289): Wrote stack traces to '/data/anr/traces.txt'
  04-19 02:47:02.302: I/dalvikvm(1289): threadid=3: reacting to signal 3
  04-19 02:47:02.443: I/dalvikvm(1289): Wrote stack traces to '/data/anr/traces.txt'
  04-19 02:47:02.811: I/dalvikvm(1289): threadid=3: reacting to signal 3
  04-19 02:47:02.999: I/dalvikvm(1289): Wrote stack traces to '/data/anr/traces.txt'
  04-19 02:47:03.409: I/dalvikvm(1289): threadid=3: reacting to signal 3
  04-19 02:47:03.583: I/dalvikvm(1289): Wrote stack traces to '/data/anr/traces.txt'
  04-19 02:47:03.870: I/dalvikvm(1289): threadid=3: reacting to signal 3
  04-19 02:47:04.029: I/dalvikvm(1289): Wrote stack traces to '/data/anr/traces.txt'
  04-19 02:47:04.410: I/dalvikvm(1289): threadid=3: reacting to signal 3
  04-19 02:47:04.529: I/System.out(1289): B
  04-19 02:47:04.603: I/dalvikvm(1289): Wrote stack traces to '/data/anr/traces.txt'
  04-19 02:47:04.850: I/dalvikvm(1289): threadid=3: reacting to signal 3
  04-19 02:47:05.020: D/AndroidRuntime(1289): Shutting down VM
  04-19 02:47:05.020: W/dalvikvm(1289): threadid=1: thread exiting with uncaught exception (group=0x409c01f8)
  04-19 02:47:05.101: I/dalvikvm(1289): Wrote stack traces to '/data/anr/traces.txt'
  04-19 02:47:05.460: I/dalvikvm(1289): threadid=3: reacting to signal 3
  04-19 02:47:05.510: E/AndroidRuntime(1289): FATAL EXCEPTION: main
  04-19 02:47:05.510: E/AndroidRuntime(1289): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.chriswins2much.Util/com.chriswins2much.Util.LogMeInClientActivity}: android.os.NetworkOnMainThreadException
  04-19 02:47:05.510: E/AndroidRuntime(1289):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
  04-19 02:47:05.510: E/AndroidRuntime(1289):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
  04-19 02:47:05.510: E/AndroidRuntime(1289):     at android.app.ActivityThread.access$600(ActivityThread.java:123)
  04-19 02:47:05.510: E/AndroidRuntime(1289):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
  04-19 02:47:05.510: E/AndroidRuntime(1289):     at android.os.Handler.dispatchMessage(Handler.java:99)
  04-19 02:47:05.510: E/AndroidRuntime(1289):     at android.os.Looper.loop(Looper.java:137)
  04-19 02:47:05.510: E/AndroidRuntime(1289):     at android.app.ActivityThread.main(ActivityThread.java:4424)
  04-19 02:47:05.510: E/AndroidRuntime(1289):     at java.lang.reflect.Method.invokeNative(Native Method)
  04-19 02:47:05.510: E/AndroidRuntime(1289):     at java.lang.reflect.Method.invoke(Method.java:511)
  04-19 02:47:05.510: E/AndroidRuntime(1289):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
  04-19 02:47:05.510: E/AndroidRuntime(1289):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
  04-19 02:47:05.510: E/AndroidRuntime(1289):     at dalvik.system.NativeStart.main(Native Method)
  04-19 02:47:05.510: E/AndroidRuntime(1289): Caused by: android.os.NetworkOnMainThreadException
  04-19 02:47:05.510: E/AndroidRuntime(1289):     at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1099)
  04-19 02:47:05.510: E/AndroidRuntime(1289):     at java.net.InetAddress.lookupHostByName(InetAddress.java:391)
  04-19 02:47:05.510: E/AndroidRuntime(1289):     at java.net.InetAddress.getAllByNameImpl(InetAddress.java:242)
  04-19 02:47:05.510: E/AndroidRuntime(1289):     at java.net.InetAddress.getByName(InetAddress.java:295)
  04-19 02:47:05.510: E/AndroidRuntime(1289):     at com.chriswins2much.Util.LogMeInClientActivity.onCreate(LogMeInClientActivity.java:37)
  04-19 02:47:05.510: E/AndroidRuntime(1289):     at android.app.Activity.performCreate(Activity.java:4465)
  04-19 02:47:05.510: E/AndroidRuntime(1289):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
  04-19 02:47:05.510: E/AndroidRuntime(1289):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
  04-19 02:47:05.510: E/AndroidRuntime(1289):     ... 11 more
  04-19 02:47:05.779: I/dalvikvm(1289): Wrote stack traces to '/data/anr/traces.txt'
  04-19 02:47:06.839: I/dalvikvm(1289): threadid=3: reacting to signal 3
  04-19 02:47:07.000: I/dalvikvm(1289): Wrote stack traces to '/data/anr/traces.txt'
  </code></pre>
  
  <p>!
  Note: Here is a new, new updated code. Force closes are fixed!!! Thank you idiottiger!
  There is still a problem though. It's not connecting to the port I have set up on localhost:4000 in java. I've tried it non-Android and it works fine. Here is my code to fix the Force close:</p>
  
  <pre><code>public void onCreate(View v){
      // Code...
          new Thread(new Runnable() {
              public void run(){
                  try {
                      hostIp = InetAddress.getByName(hostName);
                      localIp = InetAddress.getLocalHost();
                      System.out.println("Local Ip: " + localIp.toString());
                      System.out.println("Host Ip: " + hostIp.toString());
                  } catch (UnknownHostException e) {
                      e.printStackTrace();
                  }
                  try {
                      s = new Socket(hostIp, port);
                      out = new PrintWriter(s.getOutputStream(), true);
                      in = new Scanner(s.getInputStream());
                  } catch (IOException e) {
                      e.printStackTrace();
                  }
              }
          }).start();
  
  
  }
  </code></pre>
  
  <p>New logcat below (the latter half of it):</p>
  
  <pre><code>04-19 03:38:02.920: I/System.out(1490): Local Ip: localhost/127.0.0.1
  04-19 03:38:03.099: I/System.out(1490): Host Ip: localhost/127.0.0.1
  04-19 03:38:05.420: W/System.err(1490): java.net.ConnectException: failed to connect to localhost/127.0.0.1 (port 4000): connect failed: ECONNREFUSED (Connection refused)
  04-19 03:38:05.711: W/System.err(1490):     at libcore.io.IoBridge.connect(IoBridge.java:114)
  04-19 03:38:05.711: W/System.err(1490):     at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:192)
  04-19 03:38:05.781: W/System.err(1490):     at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
  04-19 03:38:05.781: W/System.err(1490):     at java.net.Socket.startupSocket(Socket.java:566)
  04-19 03:38:05.803: W/System.err(1490):     at java.net.Socket.&lt;init&gt;(Socket.java:225)
  04-19 03:38:05.809: W/System.err(1490):     at com.chriswins2much.Util.LogMeInClientActivity$1.run(LogMeInClientActivity.java:48)
  04-19 03:38:05.839: W/System.err(1490):     at java.lang.Thread.run(Thread.java:856)
  04-19 03:38:05.859: W/System.err(1490): Caused by: libcore.io.ErrnoException: connect failed: ECONNREFUSED (Connection refused)
  04-19 03:38:05.925: W/System.err(1490):     at libcore.io.Posix.connect(Native Method)
  04-19 03:38:05.939: W/System.err(1490):     at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:85)
  04-19 03:38:05.979: W/System.err(1490):     at libcore.io.IoBridge.connectErrno(IoBridge.java:127)
  04-19 03:38:05.979: W/System.err(1490):     at libcore.io.IoBridge.connect(IoBridge.java:112)
  04-19 03:38:05.989: W/System.err(1490):     ... 6 more
  04-19 03:38:06.692: D/gralloc_goldfish(1490): Emulator without GPU emulation detected.
  </code></pre>