简体   繁体   English

无法从View MainActivity.java类型静态引用非静态方法invalidate()

[英]Cannot make a static reference to the non-static method invalidate() from the type View MainActivity.java

I'm getting an error in eclipse stating: "Cannot make a static reference to the non-static method invalidate() from the type View MainActivity.java" and I'm not sure exactly what I can do to resolve this. 我在eclipse中遇到错误,指出:“无法从View MainActivity.java类型对非静态方法invalidate()进行静态引用”,我不确定该如何解决。 How can I either make invalidate() static or make View non-static? 如何将invalidate()设为静态或将View设为非静态?

SOURCE: 资源:

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:

<?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> 

FIRST SOLUTION ATTEMPT: 首次解决方案尝试:

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

View is a class. View是一类。 invalidate() is an instance method. invalidate()是一个实例方法。 You need to call it on an instance. 您需要在实例上调用它。

However, you need to perform UI modifications on the UI thread . 但是, 您需要在UI线程上执行UI修改 I would make your Activity class implement a Callback interface with a method like 我会让您的Activity类使用类似的方法实现Callback接口

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

Your class then implements it as 然后您的课程将其实现为

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

Then pass that to DownloadWebPageTask . 然后将其传递给DownloadWebPageTask In other words, create a constructor that accepts a Callback instance 换句话说,创建一个接受Callback实例的构造函数

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

private Callback callback;

...

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

UI modifications need to be done of the UI thread. 需要对UI线程进行UI修改。 That's why you need the runOnUiThread() method. 这就是为什么需要runOnUiThread()方法的原因。

invalidate() is a non static method in the VIEW class. invalidate()是VIEW类中的一个非静态方法。 you need to invoke that method after creating any instance of View or any of its child . 您需要在创建View的任何实例或其任何子级之后调用该方法。 In this case you can try invalidating the textView or root view of your activity. 在这种情况下,您可以尝试使活动的textView或root视图无效。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 类型无法从类型View静态引用非静态方法setVisibility(int) - Type Cannot make a static reference to the non-static method setVisibility(int) from the type View 无法从类型View中静态引用非静态方法findViewById(int) - Cannot make a static reference to the non-static method findViewById(int) from the type View Java - 无法从类型“something”中对非静态方法“something”进行静态引用 - Java - Cannot make a static reference to the non-static method "something" from the type "something" 无法使 static 引用非静态方法 --- 从类型 --- Java Bukkit Minecraft - Cannot make a static reference to the non-static method --- from the type --- Java Bukkit Minecraft 无法静态引用非静态方法(Java) - Cannot make static reference to the non-static method (Java) Java错误:无法对非静态方法进行静态引用 - Java Error: Cannot make a static reference to the non-static method 无法解析webView且无法从Activity类型静态引用非静态方法findViewById(int) - webView cannot be resolved & cannot make a static reference to the non-static method findViewById(int) from the type Activity 错误:“无法从类型Activity中对非静态方法startActivity(Intent)进行静态引用” - Error: “Cannot make a static reference to the non-static method startActivity(Intent) from the type Activity” 无法从类型XHTMLImporter静态引用非静态方法setHyperlinkStyle(String) - Cannot make a static reference to the non-static method setHyperlinkStyle(String) from the type XHTMLImporter 无法从类型Cmd对非静态方法getVideoURL()进行静态引用 - Cannot make a static reference to the non-static method getVideoURL() from the type Cmd
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM