在AsyncTask的onPostExecute方法中使用Intent发送字符串时出错

时间:2013-12-31 00:24:13

标签: android android-intent android-asynctask

基本上我正在尝试使用intent将wordName字符串发送到其他活动。但是,尝试在onPostExecute方法中创建新的Intent时出错。 这是AsyncTask的代码:

private class Fetcher extends AsyncTask<Void,Void, Void>{
   private Element data=null; //Initialise element data
   private Element data2=null; //Initialise element data2
   private Element define=null; //Initialise element define
   private Element example =null; //Initialise element example
   private String wordName;
   private String description;
   private String examp;


   @Override
   protected Void doInBackground(Void... arg0) { 
        Document doc = null;
        try {
            doc = Jsoup.connect("http://www.urbandictionary.com/").get(); //Connect to the url and get html document
            data = doc.getElementsByClass("word").first(); //Get the first element class named word
            data2 = data.select("a[href]").first(); //Get the word name associated with the href
            wordName = data2.text(); //Get word name text and assign it to string wordName

            define = doc.getElementsByClass("definition").first(); //Get the first element class named definition
            description = define.text(); //get the text from definition class and assign it to description

            example = doc.getElementsByClass("example").first(); //Get the first element class named example
            examp = example.text(); //get the text from example class and assign it to examp

        }
        catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }


    @Override
    protected void onPostExecute(Void result) {
        t = (TextView) DailyWord.v.findViewById(R.id.textView1); //Assign Textview in DailyWord to TextView in the xml file
        t2 = (TextView) DailyWord.v.findViewById(R.id.textView4); //Assign Textview in DailyWord to TextView in the xml file
        t3 = (TextView) DailyWord.v.findViewById(R.id.textView6); //Assign Textview in DailyWord to TextView in the xml file
        t3.setText(examp); //Set text of textView3 to string examp, also removes placeholder text
        t2.setText(description); //Set text of textView2 to string description, also removes placeholder text
        t.setText(wordName); //Set text of textView1 to string wordName, also removes placeholder text
       /*
        *THIS IS WHERE THE ERROR OCCURS
        */
        Intent sendWord = new Intent(DailyWord.this,WordWidget.class);
        sendWord.putExtra("word", wordName);
        startActivity(sendWord);

     }
}

0 个答案:

没有答案