As a novice Android developer, I’ve faced a bit strange problem. I want to create a class, which methods other classes-activities-whatever could use for working with files in some special way. Let`s say for simplicity we would be logging some stuff. If I do following within an activity (in OnClick listener for example), everything works just fine:
FileOutputStream fOut = openFileOutput("somefile", MODE_PRIVATE);
OutputStreamWriter osw = new OutputStreamWriter(fOut);
osw.write("Very important foobar");
osw.flush();
osw.close();
But when I try to enclose that into some class and create singleton like that:
public class Logger extends BaseActivity {
//BaseActivity is the "init" class which extends Activity
public static final Logger INSTANCE = new Logger();
private Logger() {
// singleton
}
public boolean doLog (String whatToLog) {
try {
FileOutputStream fOut = openFileOutput("somefile", MODE_PRIVATE);
OutputStreamWriter osw = new OutputStreamWriter(fOut);
osw.write(whatToLog);
osw.flush();
osw.close(); }
catch (IOException ioe) { ioe.printStackTrace(); }
return true; }
and call it from other activity like that
Logger.INSTANCE.doLog("foobar");
app chrashes with NullPointerException (at line with openFileOutput). I suppose it`s because of improper use of singleton/activity here and now rewriting the code to run as a service. But maybe there are some better ideas to solve an issue? Or some workarounds?
Thanks for your contributions in advance!
You based your singleton on an Activity that you didn’t start as an Activity. Therefore, it doesn’t have a valid Context, which is necessary for the IO calls. See Blundell’s answer for better singleton, with one change: As per android.app.Application javadoc, your singleton should get the application context from the given context via Context.getApplicationContext(). See why does AndroidTestCase.getContext().getApplicationContext() return null for another example.