该方法findViewById(INT)是不确定的,使用的AsyncTask为Jsoup解析器类型 [英] The method findViewById(int) is undefined for the type using AsyncTask for Jsoup Parser

查看:145
本文介绍了该方法findViewById(INT)是不确定的,使用的AsyncTask为Jsoup解析器类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图用jsoup来查询网址,我得到以下错误:该方法findViewById(INT)是未定义类型

有什么建议? (我真的不知道我在做什么错在这一点上还是问题如何进行纠正)

使用例如:

如何使用的AsyncTask为Jsoup分析器?

来源:

 包com.example.httpgetandroidexample;进口java.io.IOException异常;进口org.jsoup.Jsoup;
进口org.jsoup.nodes.Document;
进口org.jsoup.nodes.Element;
进口org.jsoup.select.Elements;进口android.app.Activity;
进口android.app.ProgressDialog;
进口android.content.Context;
进口android.os.AsyncTask;
进口android.os.Bundle;
进口android.util.Log;
进口android.widget.TextView;公共类HttpGetAndroidExample< AsyncronoustaskAndroidExample>扩展
        活动{    / **当第一次创建活动调用。 * /
    @覆盖
    公共无效的onCreate(捆绑savedInstanceState){        super.onCreate(savedInstanceState);
        的setContentView(R.layout.main);    }
}公共类HttpGetAndroidExample< OnCompleteListener>扩展
        AsyncTask的<太虚,太虚,太虚> {
    //此处声明变量您使用用于分析
    私人元素概述= NULL;
    私人元featureList = NULL;
    私人拥有的元素= NULL;
    私人元款= NULL;
    字符串URL =htt​​p://www.sheriff.org/apps/arrest/results.cfm?lname=&fname=;    @覆盖
    保护无效doInBackground(虚空......为arg0){
        文档DOC = NULL;
        尝试{
            DOC = Jsoup.connect(URL)获得();
            概述= doc.select(#DIV对象概述)最后();
            featureList = doc.select(div.callout箱)最后的()。            功能= featureList.select(「李先生」);
            ,第一款= overview.select(P)最后();
            的System.out.println(paragraph.text());
        }赶上(例外五){
            // TODO自动生成catch块
            e.printStackTrace();
        }        //获取段落元素
        // article.setText(paragraph.text());我的评论了这一点,因为你
        //无法更新从非UI线程的用户界面,因为doInBackground运行
        //在后台线程。        返回null;
    }    @覆盖
    保护无效onPostExecute(虚空结果){        TextView的文章=(的TextView)findViewById(R.id.releaseInfo);
        最终的TextView featuresText =(的TextView)findViewById(R.id.features);
        对于(元素e:功能){
            //的setText()
        }    }}


解决方案

显然,你的类不是内部类的活动,因此,你不必访问它的(活动)的方法,如findViewById。你可以做两件事情:


  1. 把HttpGetAndroidExample作为私有类的活动在

  2. 传递活动作为参数传递给HttpGetAndroidExample并调用需要的时候它的方法。

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()
        }

    }

}

解决方案

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.

这篇关于该方法findViewById(INT)是不确定的,使用的AsyncTask为Jsoup解析器类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆