【发布时间】:2010-10-27 08:48:58
【问题描述】:
文档 http://developer.android.com/guide/topics/data/data-storage.html
表明有多种方法可以保存数据,我需要在小部件中执行此操作,每次尝试保存时都会出错...
例如
SharedPreferences settings = getSharedPreferences("NAME", 0);
SharedPreferences.Editor editor = settings.edit();
editor.putBoolean("silentMode", false);
// Commit the edits!
editor.commit();
错误
Description Resource Path Location Type
The method getSharedPreferences(String, int) is undefined for the type AWidget
又一次尝试:
String FILENAME = "hello_file";
String string = "hello world!";
FileOutputStream fos = openFileOutput("Test.txt", Context.MODE_PRIVATE);
fos.write(string.getBytes());
fos.close();
有错误
Description Resource Path Location Type
The method openFileOutput(String, int) is undefined for the type AWidget
怎么了?我看没有提到这在小部件中不起作用,那么为什么这些示例对我不起作用?
保存这些数据的首选方法是什么?
【问题讨论】:
-
AWidget 的基本类型是什么?
-
正如@xandy 所说,了解您所在班级的类型很重要。
getSharedPreferences()在Context班级中定义。