I’m trying to access my application’s assets from another class file. Obviously this.getAssets() doesn’t work, so I tried passing the Context to the class as a parameter. I used a variety of ‘contexts’ – getBaseContext(), getApplicationContext(), this, but they all result in a NullPointerException.
Here is the code I am using. It runs fine inside the Activity, but I can’t work out how to make it work in an external class.
private String pickText(){
String line = null;
try {
AssetManager am = this.getAssets();
InputStream fstream = am.open("plan.txt");
DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
line = br.readLine();
for(int i = 1; i < id && line != null; i++){
line = br.readLine();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return line;
}
Any ideas?
Thanks.
Pass in the activity itself as the parameter, meaning
thisfrom your Activty.