【问题标题】:The method findViewById(int) is undefined for the type using AsyncTask for Jsoup Parser对于使用 AsyncTask for Jsoup Parser 的类型,方法 findViewById(int) 未定义
【发布时间】:2013-07-18 20:36:30
【问题描述】:

我正在尝试使用 jsoup 查询 url,但出现以下错误:方法 findViewById(int) is undefined for the type

有什么建议吗? (我真的不确定我在这一点上做错了什么或如何纠正问题)

使用示例:

How to use AsyncTask for Jsoup Parser?

来源:

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

    }

}

【问题讨论】:

  • 这段代码是在 Activity 中吗?
  • 是的 - 我在上面更新了我的源代码。

标签: java android search android-asynctask jsoup


【解决方案1】:

这是因为没有为 AsyncTask 定义 findViewById() 方法。如果您在 Activity 中使用它,可以尝试使用 YourActivity.this.findViewById()。

【讨论】:

  • 我在上面更新了我的源代码以包含将在其中运行的活动,但我收到错误 HttpGetAndroidExample 类型已定义(我很抱歉 - 我对编程非常陌生)
【解决方案2】:

假设您 AsyncTaskActivity 内,则在 AsyncTask 的外部类中声明变量:

TextView article;
TextView featuresText;

然后在onCreate()

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

然后在你onPostExecute(),你可以打电话给setText

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

此外,您可以使用runOnUiThreaddoInBackground() 中进行更改。 希望对其他人有帮助请发表评论。

【讨论】:

  • 我在上面更新了我的源代码以包含将在其中运行的活动,但我收到错误 HttpGetAndroidExample 类型已定义(我很抱歉 - 我对编程非常陌生)
  • 您发布的代码存在某些问题。 1) 类不能同名。 2) AsyncTask 类进入主类(外层)。
  • 因为你的类名需要和你的文件名一样。
  • 我认为你应该先开始学习 Java 和一些 android 的基础知识,然后处理 AsyncTask 和其他东西。
【解决方案3】:

显然你的类不是Activity的内部类,因此你无权访问它的(Activity)方法,比如findViewById。你可以做两件事:

  1. 将 HttpGetAndroidExample 作为私有类放入您的活动中
  2. 将活动作为参数传递给 HttpGetAndroidExample 并在需要时调用它的方法。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-22
    • 1970-01-01
    • 2014-01-25
    • 1970-01-01
    相关资源
    最近更新 更多