我收到一条错误消息:在遵循另一个 SO 用户关于如何修复我的基于 Android 的网络抓取工具的建议后,构造函数 MainActivity.MyTask(TextView) 未定义:
我试图创建一个构造函数:
public MyTask(TextView tv) {
// TODO Auto-generated constructor stub
}
然而,这似乎并没有解决问题(错误消失了 - 但 textview 永远不会更新。)
错误:
The constructor MainActivity.MyTask(TextView) is undefined
资源:
public class MainActivity extends Activity {
TextView tv;
final String URL = "http://news.google.com";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv = (TextView) findViewById(R.id.TextView01);
new MyTask(tv).execute(URL);
}
private class MyTask extends AsyncTask<String, Void, String> {
ProgressDialog prog;
String title = "";
@Override
protected void onPreExecute() {
prog = new ProgressDialog(MainActivity.this);
prog.setMessage("Loading....");
prog.show();
}
@Override
protected String doInBackground(String... params) {
try {
Document doc = Jsoup.connect(params[0]).get();
Element tableHeader = doc.select("tr").first();
for (Element element : tableHeader.children()) {
// Here you can do something with each element
System.out.println(element.text());
tv.setText(element.text());
}
title = doc.title();
} catch (IOException e) {
e.printStackTrace();
}
return title;
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
prog.dismiss();
tv.setText(result);
}
}
}
编辑以回应 Raghunandan 的回答:
public class MainActivity extends Activity {
Context context;
TextView tv;
final String URL = "http://news.google.com";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv = (TextView) findViewById(R.id.TextView01);
new MyTask().execute(URL);
}
private class MyTask extends AsyncTask<String, Void, String> {
ProgressDialog prog;
String title = "";
@Override
protected void onPreExecute() {
prog = new ProgressDialog(MainActivity.this);
prog.setMessage("Loading....");
prog.show();
}
@Override
protected String doInBackground(String... params) {
try {
Document doc = Jsoup.connect(params[0]).get();
Element tableHeader = doc.select("tr").first();
for (Element element : tableHeader.children()) {
// Here you can do something with each element
// System.out.println(element.text());
// tv.setText(element.text());
// //cannot update ui here.
// use Log.i instead of System.out.println
}
title = doc.title();
} catch (IOException e) {
e.printStackTrace();
}
return value;
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
prog.dismiss();
// tv.setText(result);
Document doc = Jsoup.connect(params[0]).get();
Element tableHeader = doc.select("tr").first();
for (Element element : tableHeader.children()) {
// Here you can do something with each element
System.out.println(element.text());
tv.setText(element.text());
// cannot update ui here.
}
}
}
}