-2

I am novice programming. I have a Handler like this:

 private static final Handler handler = new Handler() {

     public void handleMessage(Message msg) {
         int current = msg.arg1;
         text.setText(current+ " steps");


         double recorrido;
         recorrido= current*0.70;
         DecimalFormat df = new DecimalFormat("###.#");

         String distancia_recorrida = String.valueOf(df.format(recorrido));
         distancia.setText(distance);
         if(recorrido>100){
            distancia.setText("Fin");
            vibrate();
         }

     }
 };

My vibrate method is this:

 protected static  void vibrar() {
        // TODO Auto-generated method stub
         Vibrator vibe = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE) ;
         vibe.vibrate(1000);

    }

But I have an issue because it says that "Cannot make a static reference to the non-static method getSystemService(String) from the type Activity", how can I solve it? Thank you

1 Answers1

0

getSystemService() must be called on an instance of Activity. Your Handler static member is probably within some sub-class of Activity; if you change handler to a non-static member your code will most likely work-- your anonymous Handler class will then have non-static access to its enclosing class.

codeMagic
  • 44,549
  • 13
  • 77
  • 93
antlersoft
  • 14,636
  • 4
  • 35
  • 55