Can I use the method getFilesDir() in a .class that use extends View?
public void save(){
try {
File myDir = new File(getFilesDir().getAbsolutePath());
FileWriter fw = new FileWriter(myDir + "/Test.txt");
fw.write(Integer.toString(score));
fw.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
just the method getFilesDir() gets a error message with one quick fix available – create method getFilesDir(). And this is because it’s in a class with extends View, I can fix this by putting the method in an Activity class and use the method in the View class by using static. This don’t work ether, the program crash and the score doesn’t get saved.
Just pass the Context in to your View constructor and you’ll be able to get the sdcard location.
[Edit]
What I mean is that this class of yours that extends View probably doesn’t exist anywhere where it has access to the context natively so you need to pass it that object in the constructor FROM when you instantiate it in your Activity (where you DO have a reference to it).
so something like this…
then in your Activity