2012-03-13 52 views
1

所以基本上我試着使用的AsyncTask從網站上的一些數據來分析,我希望它得到程序的網站網址,更新UI與下載的數據並顯示progressdialog(紡車),所以我想我需要做的這樣的:(UPDATE):現在ķ其確定與變量,但程序強制關閉反正的AsyncTask和JSOUP解析

private class backgroundDATA extends AsyncTask<String, Void, Void> { 

    ProgressDialog dialog; 
    Document doc; 

    @Override 
    protected void onPreExecute() { 

     dialog = dialog.show(Result.this, " ", 
       " Loading. Please wait ... ", true); 
    } 

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

     try { 

      doc = Jsoup.connect(params[0]).get(); 


     } catch (IOException e){ 
      e.printStackTrace(); 
     } 

     return null; 
    } 




protected void onPostExecute(Void result) { 


     Elements maine; 
     Elements titleJSOUP; 
     Elements recipeJSOUP; 
     Elements instructionsJSOUP; 

     String recipE; 

     maine = doc.select("div#recipeContent"); 

     titleJSOUP = doc.select("title"); 

     recipeJSOUP = maine.select("ul.recipe"); 

     instructionsJSOUP = maine.select("p.instructions"); 



     recipE = recipeJSOUP.toString(); 


     drinkNameText.setText("THE " 
       + Jsoup.parse(titleJSOUP.toString()).text() 
         ); 



     dontListenText.setText(Jsoup.parse(titleJSOUP.toString()).text() 
         ); 

     recipeText.setText(prepareDRINK(recipE)); 

     instructionsText.setText(Jsoup.parse(instructionsJSOUP.toString()) 
       .text()); 

     dialog.dismiss(); 


    } 

}

+0

你不能在onPostExecute期間訪問'backgroundDATA'的成員嗎?主,doc,imgURL等? – Tim 2012-03-13 19:21:42

+0

它顯示我它不能被解析爲變量,聲明它在這個asynctask之外使程序力量關閉 – mrp1nk 2012-03-13 19:31:57

+0

你確定它是像'main'等成員不能解決嗎?它似乎更有可能是像'drinkNameText'這樣的UI元素,除非你的'AsyncTask'被定義爲'Activity'中的內部類,否則這些元素無法解析。 – Squonk 2012-03-13 19:38:13

回答

0

那麼這將導致一個開始一個NullPointerException ...

@Override 
protected void onPreExecute() { 

    dialog = dialog.show(Result.this, " ", 
      " Loading. Please wait ... ", true); 
} 

不能調用dialog.show(...)dialognull

調用靜態ProgressDialog.show(...)方法來代替。

+0

好的部分問題的解決 - 顯示進度對話框但經過一段時間它消失了,應用程序導致強制關閉,此後我無法在eclipse中看到logcat – mrp1nk 2012-03-13 20:06:33