Hello I am having trouble accessing the acceleration data from an accelerometer class I made.
Here is the code I am using. i have an accelerometer class in a seperate file which does all the accelerometer stuff and I have another class that uses the acceleration data.
There are no errors. the app just crashes.
I have found the cause of the crash to be when the line acc.create(); is run.
I am sure I made a mistake in the way I am trying to get data from the class but Im not sure how to go about doing it another way.
Any ideas?
Main.java
[cpp]package com.example.bounce;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.view.Menu;
public class Main extends Activity {
Accelerometer acc;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
acc.create();
acc.mSensorManager = (SensorManager)getSystemService(Context.SENSOR_SERVICE);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
@Override
protected void onResume() {
super.onResume();
// acc.onResume();
}
@Override
protected void onPause() {
super.onPause();
// acc.onPause();
}
}
[/cpp]
acceleromter.java
[cpp]package com.example.bounce;
import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
public class Accelerometer implements SensorEventListener{
public SensorManager mSensorManager;
public Sensor mAccelerometer;
public float[] acceleration;
public void create() {
mAccelerometer = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
// TODO Auto-generated method stub
}
@Override
public void onSensorChanged(SensorEvent event) {
acceleration[0] = event.values[0];
acceleration[1] = event.values[1];
acceleration[2] = event.values[2];
}
protected void onResume() {
mSensorManager.registerListener(this, mAccelerometer, SensorManager.SENSOR_DELAY_NORMAL);
}
protected void onPause() {
mSensorManager.unregisterListener(this);
}
}
[/cpp]
Here is the code I am using. i have an accelerometer class in a seperate file which does all the accelerometer stuff and I have another class that uses the acceleration data.
There are no errors. the app just crashes.
I have found the cause of the crash to be when the line acc.create(); is run.
I am sure I made a mistake in the way I am trying to get data from the class but Im not sure how to go about doing it another way.
Any ideas?
Main.java
[cpp]package com.example.bounce;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.view.Menu;
public class Main extends Activity {
Accelerometer acc;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
acc.create();
acc.mSensorManager = (SensorManager)getSystemService(Context.SENSOR_SERVICE);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
@Override
protected void onResume() {
super.onResume();
// acc.onResume();
}
@Override
protected void onPause() {
super.onPause();
// acc.onPause();
}
}
[/cpp]
acceleromter.java
[cpp]package com.example.bounce;
import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
public class Accelerometer implements SensorEventListener{
public SensorManager mSensorManager;
public Sensor mAccelerometer;
public float[] acceleration;
public void create() {
mAccelerometer = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
// TODO Auto-generated method stub
}
@Override
public void onSensorChanged(SensorEvent event) {
acceleration[0] = event.values[0];
acceleration[1] = event.values[1];
acceleration[2] = event.values[2];
}
protected void onResume() {
mSensorManager.registerListener(this, mAccelerometer, SensorManager.SENSOR_DELAY_NORMAL);
}
protected void onPause() {
mSensorManager.unregisterListener(this);
}
}
[/cpp]