0

我正在尝试使用 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);

 }

}

请你帮助我好吗 ????

4

1 回答 1

0

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

将 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);

 }
}
}
于 2013-06-12T04:49:18.967 回答