简体   繁体   English

无法对非静态方法进行静态引用

[英]Cannot make a static reference to a non-static method

I'm trying to call 4 methods to use as arguments in another. 我试图调用4个方法作为另一个方法的参数。 I'm getting a "Cannot make a static reference to a non-static method" error. 我得到一个“无法对非静态方法进行静态引用”错误。 My problem is that I understand why my methods aren't already static. 我的问题是我理解为什么我的方法不是静态的。

Origin class: 起源类:

public class IotdHandler extends DefaultHandler {
    private String url = "http://www.nasa.gov/rss/image_of_the_day.rss";
    private boolean inUrl = false;
    private boolean inTitle = false;
    private boolean inDescription = false;
    private boolean inItem = false;
    private boolean inDate = false;
    private Bitmap image = null;
    private String title = null;
    private StringBuffer description = new StringBuffer();
    private String date = null;


    public void processFeed() {
        try {
            SAXParserFactory factory =
                    SAXParserFactory.newInstance();
            SAXParser parser = factory.newSAXParser();
            XMLReader reader = parser.getXMLReader();
            reader.setContentHandler(this);
            InputStream inputStream = new URL(url).openStream();
            reader.parse(new InputSource(inputStream));
        } catch (Exception e) 
        {       }
    }
        private Bitmap getBitmap(String url) {
            try {
                HttpURLConnection connection =
                        (HttpURLConnection)new URL(url).openConnection();
                connection.setDoInput(true);
                connection.connect();
                InputStream input = connection.getInputStream();
                Bitmap bitmap = BitmapFactory.decodeStream(input);
                input.close();
                return bitmap;
            } catch (IOException ioe) { return null; }
        }

            public void startElement(String uri, String localName, String qName,
                    Attributes attributes) throws SAXException {
                if (localName.equals("url")) { inUrl = true; }
                else { inUrl = false; }
                if (localName.startsWith("item")) { inItem = true; }

                else if (inItem) {
                    if (localName.equals("title")) { inTitle = true; }
                    else { inTitle = false; }
                    if (localName.equals("description")) { inDescription = true; }
                    else { inDescription = false; }
                    if (localName.equals("pubDate")) { inDate = true; }
                    else { inDate = false; }
                }
            }




public void characters(char ch[], int start, int length) {
    String chars = new String(ch).substring(start, start + length);
    if (inUrl && url == null) { image = getBitmap(chars); }
    if (inTitle && title == null) { title = chars; }
    if (inDescription) { description.append(chars); }
    if (inDate && date == null) { date = chars; }
}


public Bitmap getImage() { return image; }
public String getTitle() { return title; }
public StringBuffer getDescription() { return description; }
public String getDate() { return date; }
}

calling method: 调用方法:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_daily_image);
    IotdHandler handler = new IotdHandler();
    handler.processFeed();
    resetDisplay(IotdHandler.getTitle(), IotdHandler.getDate(),
            IotdHandler.getImage(), IotdHandler.getDescription());
}

Use handler instead of IotdHandler for accessing non-static methods from IotdHandler class same as you are accessing processFeed() method from IotdHandler.: 使用handler而不是IotdHandlerIotdHandler类访问非静态方法, IotdHandlerIotdHandler访问processFeed()方法一样:

resetDisplay(handler.getTitle(), handler.getDate(),
            handler.getImage(), handler.getDescription());

You have to call. 你必须打电话。

resetDisplay(handler.getTitle(), handler.getDate(),
        handler.getImage(), handler.getDescription());

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM