The method findViewById(int) is undefined for the type using AsyncTask for Jsoup Parser

隐身守侯 提交于 2019-12-13 05:50:55

问题


I'm attempting to query a url using jsoup and I'm getting the following error: The method findViewById(int) is undefined for the type

Any suggestions? (I'm really not sure what I'm doing wrong at this point or how the issue can be corrected)

Using example:

How to use AsyncTask for Jsoup Parser?

SOURCE:

package com.example.httpgetandroidexample;

import java.io.IOException;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;

public class HttpGetAndroidExample<AsyncronoustaskAndroidExample> extends
        Activity {

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

    }
}

public class HttpGetAndroidExample<OnCompleteListener> extends
        AsyncTask<Void, Void, Void> {
    // HERE DECLARE THE VARIABLES YOU USE FOR PARSING
    private Element overview = null;
    private Element featureList = null;
    private Elements features = null;
    private Element paragraph = null;
    String url = "http://www.sheriff.org/apps/arrest/results.cfm?lname=&fname=";

    @Override
    protected Void doInBackground(Void... arg0) {
        Document doc = null;
        try {
            doc = Jsoup.connect(url).get();
            overview = doc.select("div#object-overview").last();
            featureList = doc.select("div.callout-box").last();

            features = featureList.select("li");
            paragraph = overview.select("p").last();
            System.out.println(paragraph.text());
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        // Get the paragraph element
        // article.setText(paragraph.text()); I comment this out because you
        // cannot update ui from non-ui thread, since doInBackground is running
        // on background thread.

        return null;
    }

    @Override
    protected void onPostExecute(Void result) {

        TextView article = (TextView) findViewById(R.id.releaseInfo);
        final TextView featuresText = (TextView) findViewById(R.id.features);
        for (Element e : features) {
            // setText()
        }

    }

}

回答1:


This is because findViewById() method is not defined for AsyncTask. If you're using this within an Activity, you can try using YourActivity.this.findViewById().




回答2:


Assuming you AsyncTask is inside an Activity, in your outer class of AsyncTask declare the variables:

TextView article;
TextView featuresText;

Then inside onCreate():

article = (TextView)findViewById(R.id.releaseInfo);
featuresText = (TextView)findViewById(R.id.features);

Then in you onPostExecute(), you can just call setText

article.setText("blah"); //..

Also, you may use runOnUiThread for making changes from within the doInBackground(). Hope it helps else please comment.




回答3:


Obviously your class is not inner class of the Activity, and therefore you don't have access to its (Activity) methods, such as findViewById. You can do two things:

  1. Put HttpGetAndroidExample inside your activity as private class
  2. Pass activity as parameter to HttpGetAndroidExample and call it's methods when needed.


来源:https://stackoverflow.com/questions/17733413/the-method-findviewbyidint-is-undefined-for-the-type-using-asynctask-for-jsoup

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!