当我尝试从扩展shared preference
的类中访问View
时,我收到了错误。
错误:“The method getSharedPreferences(String, int) is undefined for the type ViewforRed
”,
ViewforRed是我的班级
以下是示例代码
public class ViewforRed extends View
{
public final String PREFS_NAME = "GRAPHICS";
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
public ViewforRed(Context context)
{
super(context);
}
public void onDraw(Canvas canvas)
{
Paint paint = new Paint();
float p0,p1,p2,p3,p4,p5,p6,p7,p8,p9;
}
}
答案 0 :(得分:8)
getSharedPreferences()是Context
对象的方法。所以你可以尝试:
public class ViewforRed extends View
{
public final String PREFS_NAME = "GRAPHICS";
SharedPreferences settings;
public ViewforRed(Context context)
{
settings = context.getSharedPreferences(PREFS_NAME, 0);
super(context);
}