title: Suddenly gets android.os.NetworkOnMainThreadException
body: <p>I have a little app that I made that have worked for months, but suddenly I get a </p>
  
  <pre><code>android.os.NetworkOnMainThreadException
  </code></pre>
  
  <p>I havent changed any code, this is the line that crashes</p>
  
  <pre><code>try {   
      endPoint = InetAddress.getByName(target.getToIp());
      port = target.getPort();
      socket = new DatagramSocket();
  }
  catch(Exception e) {
  }
  </code></pre>
  
  <p>Note that I'm not sending anything I'm just creating the instances, it breaks already on the  InetAddress row.</p>
  
  <p>It wored as late as last night, all I have done on the phone is run update on Google play, it updated to latest version of Google Maps, but that cant have anything todo with it?</p>
  
  <p>It works in the emulator, only my HTC One X that breaks</p>
  
  <p>Full code here
  <a href="https://github.com/AndersMalmgren/FreePIE/tree/master/Lib/Android/FreePIE%20Android%20IMU" rel="nofollow">https://github.com/AndersMalmgren/FreePIE/tree/master/Lib/Android/FreePIE%20Android%20IMU</a></p>
  
  <p>edit:
  Manifest</p>
  
  <pre><code>&lt;manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.freepie.android.imu"
      android:versionCode="1"
      android:versionName="1.0"&gt;  
  
      &lt;uses-sdk
          android:minSdkVersion="9"
          android:targetSdkVersion="15" /&gt;
      &lt;uses-permission android:name="android.permission.INTERNET" /&gt; 
  
      &lt;application
          android:icon="@drawable/ic_launcher"
          android:label="@string/app_name"
          android:theme="@style/AppTheme"&gt;
          &lt;activity
              android:name=".MainActivity"
              android:label="@string/title_activity_main" 
              android:screenOrientation="portrait"&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>Tried to move the problem code to the thread instead, now it wont break, but the socket is set to null.</p>
  
  <pre><code>running = true;
  worker = new Thread(new Runnable() { 
      public void run(){
          try {   
              endPoint = InetAddress.getByName(target.getToIp());
              port = target.getPort();
              socket = new DatagramSocket();
          }
          catch(Exception e) {
              Exception err = e;
          }
  
          while(running) {
              try {
              sync.await();
              } catch(Exception e) {}
  
              Send();
          }
          try  {
              socket.disconnect();
          }
          catch(Exception e)  {}
      }
  }); 
  worker.start();
  </code></pre>
  
  <p><strong>update:</strong>
  Solution was to add some failback if user types a DNS instead of a IP
  <a href="https://github.com/AndersMalmgren/FreePIE/commit/e8017e02a7893d9df41e4ed67a037f016b6a7d39" rel="nofollow">https://github.com/AndersMalmgren/FreePIE/commit/e8017e02a7893d9df41e4ed67a037f016b6a7d39</a></p>