簡體   English   中英

對於類型訪存器,未定義方法findViewById(int)

[英]The method findViewById(int) is undefined for the type fetcher

我試圖使用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();
}

}

類提取程序擴展了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);

 }

}

請你幫助我好嗎 ????

您可以找到設置為活動的當前視圖層次結構的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);

 }
}
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM