Asynctask jsoup nullpointer异常

时间:2015-08-05 12:14:33

标签: android android-asynctask nullpointerexception jsoup

 public class asynctask extends AsyncTask <Void, String, Void > {
     String title;



     @Override
     protected Void doInBackground(Void... params) {
         try {
         Document document = Jsoup.connect(url).get();
         title = document.title();
         } catch (IOException e) {
             e.printStackTrace();
         }
         return null;
     }

     @Override
     protected void onPostExecute(Void result) {
     TextView campotesto = (TextView) findViewById(R.id.text);
     campotesto.setText(title);
 }
}}

每次运行时都会在settext行和我创建asynctask类的行上获取nullpointer异常

1 个答案:

答案 0 :(得分:0)

将此标题从doInBackground()中的do传递给onPostExecute();

试试这个:

 public class asynctask extends AsyncTask <Void, String, String> 
 {
    String title;

    @Override
    protected String doInBackground(Void... params) 
   {
        try 
        {
             Document document = Jsoup.connect(url).get();
             title = document.title();
        } 
        catch (IOException e)
        {
             e.printStackTrace();
        }
     return title;
     }

     @Override
     protected void onPostExecute(String title)
     {
         TextView campotesto = (TextView) findViewById(R.id.text);
         campotesto.setText(title);
     }
}