0

I am using the following code to register and unregister SensorEventListener.

 //Get the Toggle Button
         final ToggleButton tb=(ToggleButton) findViewById(R.id.activate);
         //Listener for ToggleButton
         tb.setOnClickListener(new View.OnClickListener() {


             public void onClick(View arg0) {
                 if(tb.isChecked()){
                    //Register the sensor
                     //smanager.
                     smanager.registerListener(this, smanager.getDefaultSensor.TYPE_LINEAR_ACCELERATION,SensorManager.SENSOR_DELAY_NORMAL);
                     Log.v(classname, "Sensor Listener Unregistered");
                 }
                 else{
                      //deRegister the Sensor
                     // Unregister the listener
                     smanager.unregisterListener(this);
                     Log.v(classname, "Sensor Listener Unregistered");

                 }
             }
         });


But I am getting the following error.

The method registerListener(SensorListener, Sensor, int) is not applicable for the arguments new View.onClickListener(),{},Sensor,int

I am not getting this error when writing the same code in onPause() method of the activity. What is the problem and how to correct this?

Ashwin
  • 10,386
  • 29
  • 103
  • 172

1 Answers1

6

this refers to the OnClickListener instead of your Activity.

Change it to this:

smanager.registerListener(YourActivityClass.this, smanager.getDefaultSensor.TYPE_LINEAR_ACCELERATION, SensorManager.SENSOR_DELAY_NORMAL);

Edit to answer your context comment:

registerListener() requires a SensorListener. According to your

it works in onResume()

comment, I assumed that your Activity implements the SensorListener interface. The context itself does not implement it therefor you get the same error.

fawaad
  • 341
  • 5
  • 12
WarrenFaith
  • 56,228
  • 24
  • 130
  • 145