2011-04-05 142 views
0

我有一个进度对话框类,它是我主要活动的一个子类。它本身运行良好。Android异步进度对话框延迟

public class UpdateFeedTask extends AsyncTask<Void, Void, Void> { 

    ProgressDialog loading; 
    @Override 
    protected void onPreExecute() { 
     loading = ProgressDialog.show(NewsFeedActivity.this,"Please wait...", "Retrieving data ...", true); 
    } 

    @Override 
    protected void onProgressUpdate(Void... progress) { 

    } 


    @Override 
    protected void onPostExecute(Void result) { 

     loading.dismiss(); 
    } 

    @Override 
    protected Void doInBackground(Void... params) { 
     // TODO Auto-generated method stub 
     return null; 
    } 

} 

我有一个名为getNews的方法,它发送一个http post请求并解析结果,这需要几秒钟的时间。在这种方法中,我调用UpdateFeedTask来异步显示进度对话框,但它似乎不是同时执行的。进度对话框似乎只在http请求完成后才打开,所以不是显示进度对话框,而是发送请求,它似乎先完成方法getNews,然后在瞬间显示进度对话框。在获取数据之前,如何使对话框显示?这里是我所说的进度对话框

public void getNews(){ 

      //show the progress dialog 
    new UpdateFeedTask().execute(); 


    //go fetch some data 
    WheruClient newsFeedLocation = new WheruClient(); 

    try { 

     newsFeedArray = newsFeedLocation.getLocationActivity(sessionKey,page,filter,when); 


    }catch (ClientProtocolException e) { 
     Toast.makeText(getApplicationContext(),e.getMessage(), Toast.LENGTH_LONG).show(); 
    } catch (IOException e) { 
     Toast.makeText(getApplicationContext(),e.getMessage(), Toast.LENGTH_LONG).show(); 
    }catch (JSONException e) { 
     Toast.makeText(getApplicationContext(),e.getMessage(), Toast.LENGTH_LONG).show(); 
    } 
} 

**编辑新的异步类发送HTTP请求在后台,再返回JSON数组,但我得到一个致命的错误,上面写着

04-07 15:33 :02.967:ERROR/AndroidRuntime(11004):引起:java.lang.RuntimeException:无法在未调用Looper.prepare()的线程中创建处理程序

我读过你的例子,我想我错过了一些关于在后台工作,有什么想法?

public class UpdateFeedTask extends AsyncTask<Void, Void, JSONArray> { 


    @Override 
    protected void onPreExecute() { 
     //loading = ProgressDialog.show(NewsFeedActivity.this,"Please wait...", "Retrieving data ...", true); 
    } 

    @Override 
    protected void onProgressUpdate(Void... progress) { 

    } 


    @Override 
    protected void onPostExecute(JSONArray result) { 

     //loading.dismiss(); 
     updateFeed(result); 
    } 

    @Override 
    protected JSONArray doInBackground(Void... params) { 

     WheruClient newsFeedLocation = new WheruClient(); 

     try { 
      newsFeedArray = newsFeedLocation.getLocationActivity(sessionKey,page,filter,when); 
     } catch (ClientProtocolException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } catch (JSONException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
     return newsFeedArray; 
    } 

} 

回答

1

需要在doInBackground中调用时间密集型任务。我在doInBackground方法中看不到任何东西。请注意,不要触摸doInBackground中的用户界面。我有一些示例代码here.

+0

谢谢,我改变了结构,以执行doInBackground方法中的工作。它现在似乎打开进度对话框,去获取JSON数组,但之后迅速崩溃。不太清楚问题是什么。 – Brian 2011-04-07 22:42:21

+0

@Brian Perin也许doInBackground中的调用在该线程中抛出异常。我会将doInBackground中的调用包装成try catch e和Log.d(TAG,“msg”,e),以查看应用程序崩溃的原因。 – JAL 2011-04-07 22:54:51