tags:

views:

37

answers:

1

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;

   }

   }
+1  A: 

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

             }
ccheneson
How can I call/start an View, like I call an activity this way Intent j = new Intent(mContext, Airplane.class); startActivity(j);My activity is this for ex : ViewforRed extends View{ }
"ViewforRed extends View" is not an activity, it's a view. I think if you want to add a view, you have to use addView() from an activity. Or instantiate the view from within an activity and you can call the activity with an intent.
ccheneson
http://stackoverflow.com/questions/3365384/embed-dynamic-view-in-main-xml