getAssets (); z innej klasy
Mam prostą funkcję odczytu pliku txt.
AssetManager mngr = getAssets();
InputStream is = mngr.open("textdb.txt");
To działa z mojej głównej działalności. Ale jeśli używam tego samego kodu w oddzielnej klasie, getAssets () po prostu zwraca null / crash.
Nie jestem w stanie znaleźć, dlaczego działa tylko z głównej klasy.
Jakieś pomysły?Rozwiązanie:
subClass.ReadSettings(getApplicationContext());
public String[] ReadSettings(Context myContext) {
}
27
1 answers
Czy Twoja druga klasa również jest Activity
?
getAssets() jest metodą kontekstu. Jeśli twoja klasa nie jest aktywnością, musisz przekazać do niej kontekst, a następnie wywołać getAssets.
Like so:
public myClass(Context myContext) {
AssetManager mngr = myContext.getAssets();
InputStream is = mngr.open("textdb.txt");
}
55
Author: Sander van't Veer,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2011-12-10 17:36:35
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2011-12-10 17:36:35