Hi, when trying to start a socket connection in an AsyncTask, my
application crashes for no reason (debugger says
"android.view.ViewRoot$CalledFromWrongThreadException: Only the
original thread that created a view hierarchy can touch its views.").

If I remove the Async and use the same code, my application works (of
course, if I make the socket connect to a not valid IP or port, the
application freezes for 5 seconds, because that's the socket's
timeout).
As I'd like to avoid that 5 seconds freeze with wrong configurations,
I have to implement the AsyncTask.

The application works just fine opening the socket and everything
related, but as I try and make it change some TextViews, it gives this
error. The AsyncTask doc says:

"AsyncTask enables proper and easy use of the UI thread. This class
allows to perform background operations and publish results on the UI
thread without having to manipulate threads and/or handlers."

So I'm doing something legit.

This is the code, it's quite simple:

        private class ConnectTask extends AsyncTask<Void, Void, Void> {
            protected Void doInBackground(Void... params) {
                try {
                        InetAddress inetAddr = InetAddress.getByName(IP);
                        int PORT = Integer.parseInt(Port);

                                if (PORT < 0)
                                        throw (new NumberFormatException());
                                if (IP.equals(""))
                                        throw (new UnknownHostException());

                        SocketAddress sockaddr = new InetSocketAddress(inetAddr,
PORT);
                        socket = new Socket();
                        socket.connect(sockaddr, 5000);

                        if (socket.isConnected()) {
                                        OutputStreamOut = new
ObjectOutputStream(socket.getOutputStream());
                                        InputStreamIn = new 
ObjectInputStream(socket.getInputStream());
                        // everything worked fine until here
                                statusTxt.setText("App is now connected to:\n" 
+ IP + " : "
+ Port); // CRASHING !!!!
                                connectBt.setText("Disconnect");
                                connected = true;
                                continueBt.setVisibility(0);
                        } else {
                                throw new UnknownHostException();
                        }
                } catch (SocketTimeoutException sockExc) {
                        statusTxt.setText("Connection timeout:\nplease, review 
your IP/
Port settings.");
                } catch (NumberFormatException numExc) {
                        statusTxt.setText("Number Format Error:\nplease, review 
your
Port settings.");
                        } catch (UnknownHostException e1) {
                                statusTxt.setText("Unknown Host Error:\nplease, 
review your IP/
Port settings.");
                        } catch (IOException e2) {
                                statusTxt.setText("Input/Output Error:\nplease, 
review your
connection settings.");
                        } finally {
                                //dialog.dismiss();
                        }
            return null;
            }

            protected void onProgressUpdate(Void... progress) {}

            protected void onPostExecute(Void result) {}
         }

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to