1

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

如何通过 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);
        }
    }
}

编辑以回应 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.

            }
        }
    }
}
4

1 回答 1

0

你有这个

  new MyTask(tv).execute(URL);

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

  public MyTask(TextView tv)
  {

  }

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

您可以将文本设置在onPostExecute. 但是您正在将文本设置为doInbackground.

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

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

无法将 AsyncTask 的响应发布到 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);
                }
            }
        }

有关 asynctask 的更多信息

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>

快照

在此处输入图像描述

于 2013-09-19T17:44:52.963 回答