我無法找到這裏代碼中的錯誤。我試圖獲取acceletometer數據,但是當我嘗試在設備上運行它時,它出現了一條消息,該進程已被關閉。我的加速度計代碼有問題
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:代碼被更新,仍然面臨的問題。 UPD3:日誌貓更新
UPD4:我更新了代碼,現在它工作。也許有人會需要它。謝謝。
粘貼logcat的太 – waqaslam