Android dialog exception: java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
<p>I am having this exception:</p>

<pre><code>java.lang.RuntimeException: An error occured while executing doInBackground()
        at android.os.AsyncTask$3.done(AsyncTask.java:278)
        at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
        at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
        at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
        at java.util.concurrent.FutureTask.run(FutureTask.java:137)
        at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:208)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
        at java.lang.Thread.run(Thread.java:856)
Caused by: java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
        at android.os.Handler.&lt;init&gt;(Handler.java:121)
        at android.widget.Toast$TN.&lt;init&gt;(Toast.java:347)
        at android.widget.Toast.&lt;init&gt;(Toast.java:93)
        at android.widget.Toast.makeText(Toast.java:235)
        at com.problemio.TopicActivity$DownloadWebPageTask.doInBackground(TopicActivity.java:440)
        at com.problemio.TopicActivity$DownloadWebPageTask.doInBackground(TopicActivity.java:1)
        at android.os.AsyncTask$2.call(AsyncTask.java:264)
        at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
        ... 5 more
java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
        at android.os.Handler.&lt;init&gt;(Handler.java:121)
        at android.widget.Toast$TN.&lt;init&gt;(Toast.java:347)
        at android.widget.Toast.&lt;init&gt;(Toast.java:93)
        at android.widget.Toast.makeText(Toast.java:235)
        at com.problemio.TopicActivity$DownloadWebPageTask.doInBackground(TopicActivity.java:440)
        at com.problemio.TopicActivity$DownloadWebPageTask.doInBackground(TopicActivity.java:1)
        at android.os.AsyncTask$2.call(AsyncTask.java:264)
        at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
        at java.util.concurrent.FutureTask.run(FutureTask.java:137)
        at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:208)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
        at java.lang.Thread.run(Thread.java:856)
</code></pre>

<p>With this code:</p>

<p>Dialog dialog;</p>

<pre><code>/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) 
{
 ....

dialog = new Dialog(this);
    Button submit = (Button)findViewById(R.id.submit_comment);   
    submit.setOnClickListener(new Button.OnClickListener() 
    {  
       public void onClick(View v) 
       {
          dialog.setContentView(R.layout.please_wait);
          dialog.setTitle("Submitting Your Comment");

          TextView text = (TextView) dialog.findViewById(R.id.please_wait_text);
          text.setText("Please wait while your comment is processed... ");
          dialog.show(); //this will show dialog 

          String c = comment.getText().toString(); 

          if ( c == null || c.length() == 0 )
          {
                try {
                    dialog.dismiss();
                } catch (Exception ee) {
                    // nothing
                }
          }
          else
          {             
              ...  
          }
       }
    });


    // Go to the database and display a list of problems
sendFeedback( problem_id , recent_topic_id );   
}       

public void sendFeedback(String problem_id , String recent_topic_id) 
{  

    String[] params = new String[] 
            { "url", problem_id , recent_topic_id };

    DownloadWebPageTask task = new DownloadWebPageTask();
    task.execute(params);        
}       

public class DownloadWebPageTask extends AsyncTask&lt;String, Void, String&gt; 
{
    @Override
    protected String doInBackground(String... theParams) 
    {
        String myUrl = theParams[0];
        final String problem_id = theParams[1];
        final String solution_section = theParams[2];

        String charset = "UTF-8";           
        String response = null;

        try 
        {                           
            String query = String.format("problem_id=%s&amp;solution_section=%s", 
                     URLEncoder.encode( problem_id, charset),
                     URLEncoder.encode( solution_section, charset));

            final URL url = new URL( myUrl + "?" + query );

            final HttpURLConnection conn = (HttpURLConnection) url.openConnection();

            conn.setDoOutput(true); 
            conn.setRequestMethod("POST");

            conn.setDoOutput(true);
            conn.setUseCaches(false);

            conn.connect();

            final InputStream is = conn.getInputStream();
            final byte[] buffer = new byte[8196];
            int readCount;
            final StringBuilder builder = new StringBuilder();
            while ((readCount = is.read(buffer)) &gt; -1) 
            {
                builder.append(new String(buffer, 0, readCount));
            }

            response = builder.toString();      
        } 
        catch (Exception e) 
        {
            // What should I do here?           
        }

        return response;
    }

    @Override
    protected void onPostExecute(String result) 
    {       
        if ( result == null )
        {
            try {
                dialog.dismiss();
            } catch (Exception ee) {
                // nothing
            }                               
        }
        else
        if ( result.equals("no_suggested_solution_id") || result.equals("no_topic_id" ) )
        {               
            try {
                dialog.dismiss();
            } catch (Exception ee) {
                // nothing
            }                               
        }
        else
        { 
            if ( result.length() == 0 )
            {
                discussion.clear();

                DiscussionMessage message = new DiscussionMessage ( );
                message.setMessage("This section is empty.");

                discussion.add( message );                  
            }
            else
            {
                try
                {   
                    JSONArray obj = new JSONArray(result);

                    if ( obj != null )
                    {
                        discussion.clear();

                        TextView topic_instructions = (TextView) findViewById(R.id.topic_instructions);

                    } 
                    adapter.notifyDataSetChanged();                     
                }                   
                catch ( Exception e )
                {
                    e.printStackTrace();
                }
            }

            adapter.notifyDataSetChanged();                     
        }
    }    
</code></pre>

<p>Any idea what I am doing incorrectly with the dialog to have this exception?</p>

<p>Thanks!</p>
