方法findViewById(int)未定义类型提取器

时间:2013-06-12 04:47:01

标签: android jsoup

我试图使用jsoup从网站返回字符串并在textview中查看字符串但是我收到了这个错误

类型提取器

的方法findViewById(int)未定义

我的代码是:

public class Second extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_second);



}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.activity_second, menu);
    return true;
}

public void Iqama (View view) throws IOException
{
    //Document doc = Jsoup.connect("http://google.com/").get();
    new fetcher().execute();
}

}

类fetcher扩展AsyncTask {     String myString = null;

 @Override
    protected Void doInBackground(Void... arg0) { 
        Document doc = null;

        try {
            doc = Jsoup.connect("http://www.ismmusalla.org/").get();
            Elements divs = doc.select("div#title1");


                for (Element div : divs) {
                    myString=myString+" " +div.text();
                      }



        }
        catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;

}

 protected void onPostExecute(String result)
 {
     TextView textview=(TextView)findViewById(R.id.textView1);
     textview.setText(myString);

 }

}

你能帮我吗????

1 个答案:

答案 0 :(得分:0)

您可以找到设置为活动的当前视图层次结构的ViewById。

将textview声明为类变量。

在onCreate()

    textview=(TextView)findViewById(R.id.textView1);

制作活动类的asynctask内部类。

public class Second extends Activity {

TextView textview;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_second);
    textview=(TextView)findViewById(R.id.textView1); 
    new fetcher().execute();    
}


 class fetcher extends AsyncTask<Void,Void,Void>{
 String myString = null;
 @Override
    protected Void doInBackground(Void... arg0) { 
        Document doc = null;

        try {
            doc = Jsoup.connect("http://www.ismmusalla.org/").get();
            Elements divs = doc.select("div#title1");
                for (Element div : divs) {
                    myString=myString+" " +div.text();
                      }
        }
        catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
}
 @Override
 protected void onPostExecute(void result)
 {
     textview.setText(myString);

 }
}
}