I am using the following class to read accelerometer data from an android mobile:
public class AccelerometerData extends Activity implements SensorEventListener {
//variables
public AccelerometerData()
{
mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
mAccelerometer = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
mSensorManager.registerListener(this, mAccelerometer, SensorManager.SENSOR_DELAY_NORMAL);
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
@Override
protected void onPause() {
super.onPause();
mSensorManager.unregisterListener(this);
}
@Override
protected void onResume() {
super.onResume();
mSensorManager.registerListener(this, mAccelerometer, SensorManager.SENSOR_DELAY_NORMAL);
}
@Override
public void onSensorChanged(SensorEvent event) {
// TODO: my codes
}
}
}
Now from my main activity class, I was calling that class to read the data
AccelerometerData sbt = new AccelerometerData ();
and it shows the following message Unfortunately AccelerometerData has stopped. I am new in android development and not sure what is wrong here.
Any help would be highly appreciated.
Your class shouldn't extend Activity. It doesn't need to. It should be owned by an activity of some sort instead.
The answer you're looking for might look a little something like this. The MainActivity owns the AccelerometerData class.
Then in AccelerometerData class it might look something like this.
}