开发者

Unable to use shared preference within a class that extends View

I am getting an error , when I try to access the shared preference from within class that extends View.

The Error : "The method getSharedPreferences(String, int) is undefined for the type ViewforRed" ,

where ViewforRed is my class

Here is the sample code

 public class ViewforRed extends View

 {

       public final String PREFS_NAME = "GRAPHICS";
       SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);


           public ViewforRed(Context context)
             {
                super(c开发者_如何学编程ontext);

             }

         public void onDraw(Canvas canvas) 
               {


                 Paint paint = new Paint();

                 float  p0,p1,p2,p3,p4,p5,p6,p7,p8,p9;

   }

   }


getSharedPreferences() is a method of a Context object. So you can try:

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

             }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜