2012-11-11 29 views
0

我有此异常:Android的对话框例外:了java.lang.RuntimeException:无法里面)线程已经不叫Looper.prepare(创建处理程序

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.<init>(Handler.java:121) 
     at android.widget.Toast$TN.<init>(Toast.java:347) 
     at android.widget.Toast.<init>(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.<init>(Handler.java:121) 
     at android.widget.Toast$TN.<init>(Toast.java:347) 
     at android.widget.Toast.<init>(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) 

有了这个代码:

对话框对话框;

/** 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<String, Void, String> 
{ 
    @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&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)) > -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();      
     } 
    }  

任何想法我做错了对话框有这种异常?

谢谢!

+0

什么是写在行440? 并在异常情况下返回null(doInBackGround())。 – meh

+0

@ user1597833自那时以来,我改变了代码,不确定这里是440行,但它是围绕doInBackground的catch语句,我认为。 – Genadinik

+0

但它也可能在onPostExecute()...对不起,我不确定:(我知道它没有帮助:( – Genadinik

回答

1

我已经开始对话框中的“onPreExecute()”

protected void onPostExecute(Long result) { 
    dialog = new Dialog(YourActivityName.this); 
    //your initiation 
    dialog.show() 
} 

而doInBackground()我不得不返回空,如果有异常引发内。

并尝试在onPostExecute年初关闭该对话框()

@Override 
protected void onPostExecute(String result) 
{  
    dialog.dismiss(); 
} 

如果不帮助,然后尝试的AsyncTask移动到一个单独的类,而不是一个内部类的活动,当我将asynctask写入不同的类时,它通常适用于我。

我看到在你的代码中你调用的“sendFeedback()”不在onClick方法中。

你打电话dismiss()在asynctask开始运行后开始的对话框,也许这可能是问题?

+0

在你的第一个代码片段中,你的意思是把它作为onExExcute? – Genadinik

+0

其实我有 – Genadinik

+0

在你的代码中,对话框可以在asynctask开始运行后启动 这可能是问题的原因,将所有的对话启动移动到onPreExecute,它很可能会解决这个问题。 – meh

2

它看起来像你试图在你的doInBackground() catch-block中显示一个Toast,你不能这样做,因为你试图在不同的Thread上创建一个UI元素。

只需使用一个标志,表示发生错误,并将这个举杯onPostExecute()

boolean error = false; 
protected String doInBackground(String... theParams) 
{ 
    // same as before 
    catch (Exception e) 
    { 
     // Handle the error gracefully 
     error = true; 
    } 
} 

protected void onPostExecute(String result) 
{  
    if(error) { 
     // Show your Toast here, you have access to the UI thread 
    } 
    // etc 
} 
相关问题