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.