I have a small application that I made that worked for several months, but suddenly I get
android.os.NetworkOnMainThreadException
I have not changed the code, this is a line that breaks
try { endPoint = InetAddress.getByName(target.getToIp()); port = target.getPort(); socket = new DatagramSocket(); } catch(Exception e) { }
Please note that I am not sending anything, I am just creating instances, it is already split into the InetAddress line.
He visited last night, everything I did on the phone was updated on Google Play, it was updated to the latest version of Google Maps, but this can not have something with it?
It works in an emulator, only my HTC One X that crashes
The full code is here https://github.com/AndersMalmgren/FreePIE/tree/master/Lib/Android/FreePIE%20Android%20IMU
edit: manifest
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.freepie.android.imu" android:versionCode="1" android:versionName="1.0"> <uses-sdk android:minSdkVersion="9" android:targetSdkVersion="15" /> <uses-permission android:name="android.permission.INTERNET" /> <application android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme"> <activity android:name=".MainActivity" android:label="@string/title_activity_main" android:screenOrientation="portrait"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
I tried to move the problem code to the stream instead, now it will not break, but the socket is set to null.
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();
update: The solution was to add some rejection if the user enters DNS instead of IP https://github.com/AndersMalmgren/FreePIE/commit/e8017e02a7893d9df41e4ed67a037f016b6a7d39