Android上のシングルトン(アクティビティではない)からres/rawフォルダ内のファイルにアクセスするにはどうすればよいですか? 私が試してみた:Androidでファイルの場所を取得するためのシングルトン
ので、動作しませんInputStream is = MainActivity.getResources().openRawResource("data.json");
「(非静的メソッドgetResouces)は、静的なコンテンツから参照することはできません」。 私も試してみた:
URL fileURL = getClass().getClassLoader().getResource(R.raw.data);
String filePath = fileURL.getPath();
ヌル・ポインタ例外をスローしています。
私のシングルトン:私は、ファイルを取得したい
public class CoursesDataManager {
private static CoursesDataManager instance;
private final List<Course> courses;
public static CoursesDataManager getInstance() {
if (instance == null)
instance = new CoursesDataManager();
return instance;
}
private CoursesDataManager() {
courses = parseCourses(**filePath/inputStream**);
}
理由は変化しないとアクセスできるように決して私が私のシングルトンは、一度そのファイル内のデータを解析し、このデータを格納し、このデータを持つようにしたいということです私のアプリケーションの生涯を通して。
ありがとうございます。
あなたの非活動クラスには文脈が必要です。 – Raghunandan
それはどういう意味ですか?それはシングルトンなので、私はそれにパラメータを渡すことはできません。 –
http://developer.android.com/reference/android/content/Context.html#getResources()これをチェックして。 – Raghunandan