I am wondering does reading files in android only works under onCreate() of an activity?
I having this question because my reading process works when the reading process is in side of onCreate() or in side a method that is called under onCreate.
Following is what I am using for reading a file
try {
file="";
InputStream ish = this.getResources().openRawResource(R.drawable.regular_hero_deck);
BufferedReader brh = new BufferedReader(new InputStreamReader(ish));
if(ish!=null){
while ((data = brh.readLine()) != null) {
file+=data;
}
}
ish.close();
regularDeckHero =new Deck<Hero>("heroDeck",file);
}catch (Exception e){//Catch exception if any
System.err.println("Error : reading hero deck");}
Solution: it is the problem with
InputStream ish = this.getResources().openRawResource(R.drawable.regular_hero_deck);
this.getResources() is doesn’t give what I wanted, the resources you need is the one from your MainActivty pass this resources to this should work.
It works every where, even outside an Activity..
may be the below line
causes the problem.. because you use this (current instance which is an Activity)..
instead by getting application context refrence you can still say