gpt4 book ai didi

java - 构造函数 MainActivity.MyTask(TextView) 未定义

转载 作者:行者123 更新时间:2023-12-01 04:21:03 27 4
gpt4 key购买 nike

我收到一条错误消息:在遵循另一个 SO 用户关于如何修复我的基于 Android 的网络抓取工具的建议后,构造函数 MainActivity.MyTask(TextView) 未定义:

How To Extract Table Data Via Android

我尝试创建一个构造函数:

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

编辑拉古南丹的回答:

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.

}
}
}
}

最佳答案

你有这个

  new MyTask(tv).execute(URL);

但是在你的 asycntask 中你没有像这样的构造函数

  public MyTask(TextView tv)
{

}

如果您的 asynctask 是 Activity 类的内部类,则不需要将 textview 传递给构造函数,因为您已将 textview 声明为 Activity 类成员。

您可以在onPostExecute中设置文本。但是您正在 doInbackground 中设置文本。

doInbackground 在后台线程上调用。所以你不能在那里设置文本。您需要在 ui 线程上更新 ui。

如果 asynctask 不是内部类,则使用接口(interface)

Can't post response from AsyncTask to MainActivity

    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.textView1);
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 title;
}

@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
prog.dismiss();
tv.setText(result);
}
}
}

有关异步任务的更多信息

http://developer.android.com/reference/android/os/AsyncTask.html

编辑:

public class MainActivity extends Activity {
Context context;
ArrayList<String> aa = new ArrayList<String>();
ListView lv;
final String URL = "http://news.google.com";

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lv= (ListView) findViewById(R.id.listView1);
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()) {
aa.add(element.text().toString());
}

title = doc.title();
} catch (IOException e) {
e.printStackTrace();
}
return title;
}

@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
prog.dismiss();
ArrayAdapter<String> adapter = new ArrayAdapter<String>(MainActivity.this,android.R.layout.simple_list_item_1,aa);
lv.setAdapter(adapter);
}
}
}

activtiy_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >

<ListView
android:id="@+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" >

</ListView>

</RelativeLayout>

快照

enter image description here

关于java - 构造函数 MainActivity.MyTask(TextView) 未定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18901350/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com