2012-03-13 97 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