我的加速度计代码有问题

时间:2012-05-18 13:25:12

标签: android accelerometer

我在这段代码中找不到什么问题。我正在尝试获取重量计数据,但是当我尝试在设备上运行时,它会显示一条消息,该过程已被关闭。

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:Log cat已更新

UPD4:我更新了代码,现在可以了。也许有人需要它。感谢。

2 个答案:

答案 0 :(得分:3)

您不能将findViewById(R.id.xbox);放入您的课堂正文中。它应该在onCreate()之后转到setContentView()

问题出在代码的第18行(根据logcat)。可能就是这个:

Sensor sensor = sm.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);

因为你写: SensorManager sm;  因此传感器管理器为空,因此您无法在sm.getDefaultSensor()中使用它。

请参阅此link以了解如何初始化传感器管理器:

 public SensorActivity() {
     sm = (SensorManager)getSystemService(SENSOR_SERVICE);
     sensor = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
 }

答案 1 :(得分:1)

在onCreate()中尝试从所有这6个findViewById语句中删除开始的TextView。

改变
TextView xViewA = (TextView) findViewById(R.id.xbox);

xViewA = (TextView) findViewById(R.id.xbox);

编辑:

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);

}
..