0

我在 Eclipse 中收到错误声明:“无法从 View MainActivity.java 类型中对非静态方法 invalidate() 进行静态引用”,我不确定我能做些什么来解决这个问题。如何使 invalidate() 静态或使 View 非静态?

资源:

public class MainActivity extends Activity {
    private TextView textView;
    private Handler handler;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        textView = (TextView) findViewById(R.id.TextView01);
        handler = new Handler();
    }

    private class DownloadWebPageTask extends AsyncTask<String, Void, String> {
        @Override
        protected String doInBackground(String... urls) {
           // perform HTTP client operation
        }

        @Override
        protected void onPostExecute(final String title) {

            handler.post(new Runnable() {
                    @Override
                    public void run() {
                        textView.setText(title);
                        View.invalidate();
                    }
            });
        }
    }

    public void onClick(View view) {
        DownloadWebPageTask task = new DownloadWebPageTask();
        task.execute(new String[] { "http://www.google.com" });

    }
}

XML:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <Button
        android:id="@+id/readWebpage"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="onClick"
        android:text="Load Webpage" >
    </Button>

    <TextView
        android:id="@+id/TextView01"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="Placeholder" >
    </TextView>

</LinearLayout> 

第一个解决方案尝试:

    public class MainActivity extends Activity {
    private TextView textView;
   // String response;
    public interface Callback {
        void onModifiedTextView(String value);
    }
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        textView = (TextView) findViewById(R.id.TextView01);
    }

    public void onModifiedTextView(final String title) {
        runOnUiThread(new Runnable() {
            public void run() {
                textView.setText(title);
                textView.invalidate(); // not even necessary
            }
        });
    }

    public class DownloadWebPageTask extends AsyncTask<String, Void, String> {
        public DownloadWebPageTask(MainActivity mainActivity) {
            this.callback = mainActivity;
        }

        private MainActivity callback;

        public DownloadWebPageTask() {
            // TODO Auto-generated constructor stub
        }

        public DownloadWebPageTask(TextView textView) {
            // TODO Auto-generated constructor stub
        }

        @Override
        protected String doInBackground(String... urls) {
            String response = "";

            for (String url : urls) {
                DefaultHttpClient client = new DefaultHttpClient();
                HttpGet httpGet = new HttpGet(url);
                try {
                     Document doc = Jsoup.connect("http://google.com")
                                .userAgent("Mozilla")
                                .get();
                    // get page title
                    String title = doc.title();
                    System.out.println("title : " + title);

                    // get all links
                    Elements links = doc.select("a[href]");
                    for (Element link : links) {

                        // get the value from href attribute
                        System.out.println("\nlink : " + link.attr("href"));
                        System.out.println("text : " + link.text());

                    }

                } catch (IOException e) {
                    e.printStackTrace();
                }


            }
            callback.onModifiedTextView(response);
            return response;
        }

        @Override
        protected void onPostExecute(final String title) {
            callback.onModifiedTextView(title);
        }  
    }

    public void onClick(View view) {

        DownloadWebPageTask task = new DownloadWebPageTask(this);
        task.execute(new String[] { "http://www.google.com" });
    }
}
4

3 回答 3

4

View是一类。invalidate()是一个实例方法。您需要在实例上调用它。

但是,您需要在 UI 线程上执行 UI 修改。我会让你的Activity类实现一个Callback带有类似方法的接口

public interface Callback {
    void onModifiedTextView(String value);
}

然后你的类将它实现为

public void onModifiedTextView(final String title) {
    runOnUiThread(new Runnable() {
        public void run() {
            textView.setText(title);
            textView.invalidate(); // not even necessary
        }
    });
}

然后将其传递给DownloadWebPageTask. 换句话说,创建一个接受Callback实例的构造函数

public DownloadWebPageTask(Callback callback) {
    this.callback = callback;
}

private Callback callback;

...

protected void onPostExecute(final String title) {
    callback.onModifiedTextView(title);
}  

UI 修改需要对 UI 线程进行。这就是为什么你需要这个runOnUiThread()方法。

于 2013-09-11T14:14:04.523 回答
0

查看有关实例和类方法的Java 教程,其中对此进行了详尽的解释。

另外,请参阅另一个“无法进行静态引用...”问题Java 错误:无法对非静态方法进行静态引用

于 2013-09-11T14:17:54.167 回答
0

invalidate() 是 VIEW 类中的非静态方法。您需要在创建 View 的任何实例或其任何 child 之后调用该方法。在这种情况下,您可以尝试使您的活动的 textView 或根视图无效。

于 2013-09-11T14:19:48.240 回答