0

我有这个例外:

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();                     
        }
    }    

知道我在对话框中做错了什么以产生此异常吗?

谢谢!

4

2 回答 2

2

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

只需使用标志来指示发生错误并将此 Toast 移动到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
}
于 2012-11-11T20:56:45.017 回答
1

我会在“onPreExecute()”中启动对话框

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

在 doInBackground() 内部,如果抛出异常,我会返回 null 。

并尝试在 onPostExecute() 开头关闭对话框

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

如果这没有帮助,那么尝试将 asynctask 移动到一个单独的类,而不是活动的内部类,当我在不同的类中编写 asynctask 时,它通常对我很好。

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

您在 asynctask 开始运行后开始的对话框上调用dismiss(),也许这可能是问题所在?

于 2012-11-11T20:48:24.953 回答