0

What I am trying to do should be simple: I am trying to use Android Studio to read an XML file and write the data to a database.

A simplified version of my code goes like this:

MainActivity.java:

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        try {
            XMLReader r = new XMLReader();
            r.Reader(getXML());
        } catch (XmlPullParserException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    //Creates each station
    public void newStation(int stationID, String stationName)
    {
        DBHandler dbHandler = new DBHandler(this, null, null, 1);

        Station station = new Station(stationID, stationName);

        dbHandler.addStation(station);
    }

    private String getXML()
    {
        //code to get xml
        return xml;
    }
}

XMLReader.java:

public class XMLReader {
    public void Reader (String xmlFile) throws XmlPullParserException, IOException
    {
        String[] stationData = new String[]{"",""};
        //code to cut xml file up and put it into stationData
        MainActivity.newStation(Integer.parseInt(stationData[0]), stationData[1]);
    }
}

(If you want I can add code for station.java and DBHandler.java too)

Now the issue I am having is this: "Non-static method 'newStation(int, java.lang.String)' cannot be referenced from a static context"

and the suggestion tells me to make newStation static, however if I do so then I get this issue: "'...MainActivity.this' cannot be referenced from a static context"

with the suggestion telling me to make newStation not static...

As far as I can tell newStation needs to be static in order for me to pass variables to it, however I cannot use the context 'this' in a class that is static. I tried creating a context but it felt like trying to dig myself out of a hole.

How can I get around this programming paradox? I usually am able to find an answer for any issue here but this is the first time I could not. If you know of a link with the solution I've missed please post it below. Many thanks.

L3monsta
  • 3
  • 1
  • 3
  • Where you have defined `XMLReader ` class in the same Activity class??? – Pankaj Jun 07 '15 at 06:25
  • And You cannot call a method of Activity class like YourActivity.YouActivityMethod() – Pankaj Jun 07 '15 at 06:26
  • possible duplicate of [What is the reason behind "non-static method cannot be referenced from a static context"?](http://stackoverflow.com/questions/290884/what-is-the-reason-behind-non-static-method-cannot-be-referenced-from-a-static) – Cristik Jun 07 '15 at 06:29

2 Answers2

1

You are using

MainActivity.newStation(...)

you can only do that if the newStation(...) method is defined as static, eg:

public static void newStation()

you can access it in a non-static way:

activity.newStation(...)

where activity is a reference to an MainActivity object. You can pass a reference to the MainActivity as a parameter to the XMLReader constructor, eg:

public class XMLReader {
     MainActivity activity;
     public XMLReader(MainAcrivity activity){
         this.activity = activity;
     }

     ...
}

and in the MainActivity.java:

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {

    try {
        XMLReader r = new XMLReader(this);
        r.Reader(getXML());
    } catch (XmlPullParserException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}
Titus
  • 22,031
  • 1
  • 23
  • 33
  • Making the method static is not possible. The `newStation` method uses the current `MainActivity` instance (`this`) as an argument to another call. The other suggestion will work, but from where does he get the instance? – Seelenvirtuose Jun 07 '15 at 06:32
  • @Seelenvirtuose I never suggested he should make the method `static` I was just explaining the error and he can pass a reference to the activity as a parameter to the constructor, that is his decision. – Titus Jun 07 '15 at 06:34
  • Ok, if that was not your intention, then it is not clear from your answer. You should modify it to make it much more clearer. As for the second part: OP is obviously new to programming. Leaving him _a choice_ that he can't see and explicitely is asking for doesn't fit for a good answer. Please update, and I'll remove my downvote. – Seelenvirtuose Jun 07 '15 at 06:37
1

You already have an object of type MainActivity. This one is calling the method to read the XML.

By the way, please stick to Java Code Conventions and start method names with lowercase letters. Additionally, methods always do something, so they should be verbs:

public class XMLReader {
    public void read (String xmlFile) ...
}

The easiest way to get around is to return the parsed data and call the newStation method from inside MainActivity:

public class XMLReader {
    public String[] read (String xmlFile) throws XmlPullParserException, IOException {
        String[] stationData = new String[]{"",""};
        //code to cut xml file up and put it into stationData
        return stationData;
    }
}

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        try {
            XMLReader r = new XMLReader();
            String[] stationData = r.read(getXML());
            newStation(Integer.parseInt(stationData[0]), stationData[1]);
        } catch (XmlPullParserException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void newStation(int stationID, String stationName) { ... }
    private String getXML() { ... }
}
Seelenvirtuose
  • 20,273
  • 6
  • 37
  • 66
  • Thank you Seelenvirtuose, I would upvote if I had the reputation. I fixed my method names as you suggested. – L3monsta Jun 07 '15 at 08:04