Thread that is not UiThread

I am using a Thread to start my ServerSocket and I with transactions that take longer than 500ms.

I think that triggers some "App is not responding" messages.

I tried to create a thread that to separate it from the UiThread.

Here is my code:

Thread socketServerThread=new Thread(new ServerSocketThread());

public class ServerSocketThread extends Thread {
    static final int SocketServerPORT = 8080;
    int count = 0;

    public void run() {
        try {
            serverSocket = new ServerSocket(SocketServerPORT);

            Toast.makeText(getApplicationContext(), "server is on", Toast.LENGTH_SHORT).show();

            while (true) {
                Socket socket = serverSocket.accept();

        } catch (IOException e) {
            // TODO Auto-generated catch block

My app crashes when I open it.

How can I solve those errors?

My LogCat:

04-11 21:57:23.864: E/AndroidRuntime(11761): FATAL EXCEPTION: Thread-11
04-11 21:57:23.864: E/AndroidRuntime(11761): java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
04-11 21:57:23.864: E/AndroidRuntime(11761):    at android.os.Handler.<init>(
04-11 21:57:23.864: E/AndroidRuntime(11761):    at android.widget.Toast.<init>(
04-11 21:57:23.864: E/AndroidRuntime(11761):    at android.widget.Toast.makeText(
04-11 21:57:23.864: E/AndroidRuntime(11761):    at com.example.imagesender.MainActivity$
04-11 21:57:23.864: E/AndroidRuntime(11761):    at


the problem is you are trying to show a toast from a non UI thread which you cannot do, remove that and it should work.

If you want to show the toast then you have to use a handler to call back to the main thread or better yet use an AsyncTask

You can use AsyncTask and implement actions you need to run on UI thread in onProgressUpdate and/or in onPostExecute functions.

Also you can use runOnUiThread to execute a piece of code on UI Thread like that `

public void run() {
    // custom thread actions
    mActivity.runOnUiThread(new Runnable() {
        public void run() {
            // code that should be run on UI Thread
    // custom thread actions


P.S.: everytime you get a crash — take a look at adb logcat output first and google any error messages about your program.

Need Your Help

Changing the behavior of YouTube API generated links

api search youtube hyperlink

My goal is to display YouTube search results in my website based on a keywords search (e.g through YouTube API or GoogleCustomSearch using, but change it so when you click on the link -