无法在扩展View的类中使用共享首选项

时间:2010-07-28 21:45:34

标签: android

当我尝试从扩展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;

   }

   }

1 个答案:

答案 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);

             }