I am unable to create UI elements from a Handler's handleMessage call
<h1>UPDATE 1</h1>

<p>I did some more debugging and found that the constructor is run from the main UI thread but the handleMessage(Message) method is run on the Thread I created, which completely contradicts my understanding of Handlers.</p>

<h1>Problem</h1>

<p>I've been working on Android for a while now and I've run into an issue that doesn't seem to be documented.  I'm not doing anything special with my code. I can't seem to get my application to create any UI elements such as Alert Dialogs and Toast messages from a Handler's handleMessage method.</p>

<p>Additionally, I get a "Thread has not called Looper.prepare()" exception.  However, I believe that I shouldn't even need to call Looper.prepare() since I'm not creating a Handler inside of the Thread object. I'm calling the constructor in the main UI thread and then passing it to the new Thread's constructor.</p>

<h2>What I Expect</h2>

<p>I should see a Toast message as well as an Alert Dialog.</p>

<h2>What Actually Happens</h2>

<p>Nothing.  I've run the debugger on the application and I find that the JVM does launch the Thread. The run method is called and the message is dispatched.  The handler handles the method and I run through all the methods of creating the AlertDialog as well as the Toast message but nothing ever shows up.</p>

<p>I've run this application on my Emulator running 4.2, my friend's Samsung Galaxy S3 running 4.0.4, and my own Droid X2 running 2.3.5.</p>

<h1>Flow of the Program</h1>

<ol>
<li>I create a Handler on the UI thread.</li>
<li>I create and start a Thread that will dispatch a message to the Handler.</li>
<li>The handler will handle the message </li>
</ol>

<h1>Source Code</h1>

<p><a href="http://pastebin.com/cRfaW7b0" rel="nofollow">http://pastebin.com/cRfaW7b0</a></p>

<pre><code>package com.nguyenmp.ErrorTest;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class MyActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        //Init the UI
        setContentView(R.layout.main);

        //Create a new handler, passing it the current activity context
        final Handler handler = new MyHandler(MyActivity.this);

        //Bind a listener to the button on the UI
        View view = findViewById(R.id.button);
        view.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                //Start a new thread that will send a message to that handler
                new MyThread(handler).start();
            }

        });
    }

    private class MyThread extends Thread {
        private final Handler mHandler;

        MyThread(Handler handler) {
            mHandler = handler;
        }

        @Override
        public void run() {
            //Dispatch message to UI thread asynchronously
            Looper.prepare();
            mHandler.dispatchMessage(mHandler.obtainMessage());
        }
    }

    private class MyHandler extends Handler {
        private final Context mContext;

        MyHandler(Context context) {
            mContext = context;
        }

        @Override
        public void handleMessage(Message msg) {
            //Show that we got the message
            AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
            builder.setTitle("HelloWorld");
            builder.setMessage("It worked!");
            builder.show();

            //Another variant of showing that we got the message
            Toast.makeText(mContext, "Message Received", Toast.LENGTH_SHORT).show();

            System.out.println("Yeah");
        }
    }
}
</code></pre>
