Android Service - Opening Socket Throws Null Exception
Solution 1:
You are calling open()
from onCreate()
. onCreate()
is called on the main thread. You shouldn't do network I/O on the main thread. You've got the separate thread thing in the wrong place. In your activity you're starting a separate thread that calls bindService()
and then loops forever (infinite loop chewing up CPU cycles, not a good thing). You don't need to call bindService()
from inside a separate thread because bindService()
is asynchronous. It only initiates the binding, it doesn't actually wait for the binding to complete. This can be done on the main thread.
Where you need to start a thread is inside your service. When your service gets created, you should start a separate thread in onCreate()
and that separate thread needs to do the network I/O (ie: open socket, read, write, etc.).
Also, you can make debugging easier by removing android:process=":remote"
from the manifest. This will allow you to set breakpoints easier in the service methods.
Solution 2:
Did you tried to put the Internet permission in your AndroidManifest.xml? Connecting to a server (even its a "local" running server) needs internet permissions i think.
Post a Comment for "Android Service - Opening Socket Throws Null Exception"