I'm trying to make a method which gets the text from a txtfile into a String. The method is working when I'm calling it from the same class that it's in (That class is an activity). But when I'm calling it from another class (an ordinary class) it needs to be static. And I don't know much about static, but the method is not working when it's static. As I need to call the method from another class, I either need the method to be non-static or fix the error when it's static. Here is the method:
public static String loadData(String inFile) {
String str = "";
try{
StringBuilder buf=new StringBuilder();
InputStream json= getAssets().open(inFile);
BufferedReader in=
new BufferedReader(new InputStreamReader(json, "UTF-8"));
while ((str=in.readLine()) != null) {
buf.append(str);
}
in.close();
return buf.toString();
} catch (Exception e) {
Log.e("er0r", e.toString());
}
return str;
}
The method is called from a class Solve.java:
public static void permutateYellowEdges(){
try {
Rotations.rotateSequence(MainActivity.loadData("tables\\topLayer\\edgePerm.rt"), States.getYellowEdgeState());
}catch(Exception e){
e.printStackTrace();
}
}
The error I get is a red line under "getAssets" which says "Non-static method getAssets can not be referenced from a static context". How could I fix this?