Cannot use shared preferences in a class that extends the view

I get an error when I try to access shared preferencefrom a class that extends View.

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

where ViewforRed is my class

Here is a 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;

   }

   }
+5
source share
1 answer

getSharedPreferences () is an object method Context. 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);

             }
+8
source

All Articles