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

时间:2013-09-19 17:33:04

标签: java android constructor

我收到一条错误说明:构建者MainActivity.MyTask(TextView)在遵循其他SO用户关于如何修复基于Android的网络抓取工具的建议后未定义:

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

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

            }
        }
    }
}

1 个答案:

答案 0 :(得分:0)

你有这个

  new MyTask(tv).execute(URL);

但是在你的asycntask中你没有像

这样的构造函数
  public MyTask(TextView tv)
  {

  }

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

您可以在onPostExecute中设置文字。但是您要在doInbackground中设置文字。

在后台线程上调用

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

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

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

有关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>

快照

enter image description here