Vishal
Vishal

Reputation: 999

SocketS in Android

Here is a sample of my code

     TextView textStatus = (TextView) findViewById(R.id.editText1);
     ServerSocket serverSocket = new ServerSocket(SERVERPORT);
     .
     .
     Socket client = serverSocket.accept();
     .
     .
     textStatus.append("TEXT");
     client.close();

This makes the android application force close. If I remove textStatus.append, than it works. Again, if I remove Socket client = ... and client.close(), it displays the TEXT on the screen.

So, the socket client = serverSocket.accept(); is affecting textStatus variable in some way.

Can anyone tell me what is wrong ?

Upvotes: 1

Views: 431

Answers (1)

Kurtis Nusbaum
Kurtis Nusbaum

Reputation: 30845

textStatus is probably null. Check to make sure that R.id.editText1 is the actual id that you're using in your XML file.

Also, any potentially long running task should not be done on the UI thread. You're going to have very bad app preformance. Please see the Painless Threading article and try moving your server communication off to a different thread. Or consider using an IntentService instead. I prefer the IntentService.

Upvotes: 1

Related Questions