Reputation: 1205
I cant find what is wrong here in this code. I am trying to get acceletometer data, but when i try to run it on device it comes out with a message, that process has been shut down.
public class SensorActivity extends Activity implements SensorEventListener {
SensorManager sm;
Sensor sensor ;
TextView yViewA = null;
TextView zViewA = null;
TextView xViewO = null;
TextView yViewO = null;
TextView zViewO = null;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
sm = (SensorManager)getSystemService(SENSOR_SERVICE);
sensor = sm.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
xViewA = (TextView) findViewById(R.id.xbox);
yViewA = (TextView) findViewById(R.id.ybox);
zViewA = (TextView) findViewById(R.id.zbox);
xViewO = (TextView) findViewById(R.id.xboxo);
yViewO = (TextView) findViewById(R.id.yboxo);
zViewO = (TextView) findViewById(R.id.zboxo);
}
@Override
protected void onResume() {
super.onResume();
sm.registerListener(this, sensor, SensorManager.SENSOR_DELAY_NORMAL);
}
@Override
protected void onStop() {
sm.unregisterListener(this);
super.onStop();
}
public void onAccuracyChanged(Sensor sensor, int accuracy) {
// TODO Auto-generated method stub
}
public void onSensorChanged(SensorEvent event) {
// TODO Auto-generated method stub
xViewO.setText("Orientation X: " + event.values[0]);
yViewO.setText("Orientation Y: " + event.values[1]);
zViewO.setText("Orientation Z: " + event.values[2]);
}
UPD:
05-18 19:42:17.570: E/AndroidRuntime(14875): android.app.SuperNotCalledException: Activity {daler.sensor/daler.sensor.SensorActivity} did not call through to super.onCreate()
05-18 19:42:17.570: E/AndroidRuntime(14875): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1933)
05-18 19:42:17.570: E/AndroidRuntime(14875): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1992)
05-18 19:42:17.570: E/AndroidRuntime(14875): at android.app.ActivityThread.access$600(ActivityThread.java:127)
05-18 19:42:17.570: E/AndroidRuntime(14875): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1158)
05-18 19:42:17.570: E/AndroidRuntime(14875): at android.os.Handler.dispatchMessage(Handler.java:99)
05-18 19:42:17.570: E/AndroidRuntime(14875): at android.os.Looper.loop(Looper.java:137)
05-18 19:42:17.570: E/AndroidRuntime(14875): at android.app.ActivityThread.main(ActivityThread.java:4441)
05-18 19:42:17.570: E/AndroidRuntime(14875): at java.lang.reflect.Method.invokeNative(Native Method)
UPD2: Code is updated, still facing problem. UPD3:Log cat updated
UPD4: I updated the code, now it works. Maybe someone will need it. Thanks.
Upvotes: 0
Views: 283
Reputation: 8812
In you onCreate() try removing the beginning TextView from all those 6 findViewById statements.
change from
TextView xViewA = (TextView) findViewById(R.id.xbox);
to
xViewA = (TextView) findViewById(R.id.xbox);
EDIT:
public class SensorActivity extends Activity implements SensorEventListener {
SensorManager sm;
Sensor sensor;
TextView xViewA = null;
TextView yViewA = null;
TextView zViewA = null;
TextView xViewO = null;
TextView yViewO = null;
TextView zViewO = null;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
sm = (SensorManager)getSystemService(SENSOR_SERVICE);
sensor= sm.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
sm.registerListener(this, sensor, SensorManager.SENSOR_DELAY_NORMAL);
xViewA = (TextView) findViewById(R.id.xbox);
yViewA = (TextView) findViewById(R.id.ybox);
zViewA = (TextView) findViewById(R.id.zbox);
xViewO = (TextView) findViewById(R.id.xboxo);
yViewO = (TextView) findViewById(R.id.yboxo);
zViewO = (TextView) findViewById(R.id.zboxo);
setContentView(R.layout.main);
}
..
Upvotes: 1
Reputation: 13154
You can't put findViewById(R.id.xbox);
in your class body. It should go to onCreate()
after setContentView()
.
The problem is on line 18 of your code (according to logcat). It's probably this one:
Sensor sensor = sm.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
Because you write:
SensorManager sm;
so the sensor manager is null so you can't use it in sm.getDefaultSensor()
.
See this link to find out how to initalize sensor manager:
public SensorActivity() {
sm = (SensorManager)getSystemService(SENSOR_SERVICE);
sensor = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
}
Upvotes: 3