vendredi 20 février 2015

Android - Read properties file without using context



I have a simple class which is not an activity, so I don't have access to context, but I want to read a properties file in my project (rather than on sd card).



MyClass {

public static void readProperties(){
Properties props=new Properties();
InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("/mine.properties");
try {
props.load(inputStream);
} catch(Exception e){
e.printStackTrace();
}
}
}


When I run the code though, I get a null pointer exception:



02-20 15:32:38.396: E/AndroidRuntime(27473): java.lang.NullPointerException: in == null

02-20 15:32:38.396: E/AndroidRuntime(27473): at java.util.Properties.load(Properties.java:246)



Where should I put my properties file in order to be able to read it? I tried src , the project root, assets, and also next to MyClass.java, each time getting null pointers. What am I doing wrong?


I'm using Eclipse with ADT (not on Android Studio yet).

My structure is something like this:



-src

--main/SomeActivity.java

--main/MyClass.java

--main/mine.properties

-assets

-gen

-res





Aucun commentaire:

Enregistrer un commentaire