Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to use shared preference within a class that extends View

Tags:

android

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

             }

         public void onDraw(Canvas canvas) 
               {


                 Paint paint = new Paint();

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

   }

   }
like image 547
David Prun Avatar asked Jul 28 '10 21:07

David Prun


1 Answers

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

             }
like image 168
ccheneson Avatar answered Nov 09 '22 03:11

ccheneson