Android 我的加速计代码有问题

Android 我的加速计代码有问题,android,accelerometer,Android,Accelerometer,我在这段代码中找不到什么错误。我正在尝试获取加速度计数据,但当我尝试在设备上运行它时,它会显示一条消息,该过程已被关闭 public class SensorActivity extends Activity implements SensorEventListener { SensorManager sm; Sensor sensor ; TextView yViewA = null; TextView zViewA = null; TextView xViewO = null; Tex

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

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:

UPD2:代码已更新,仍面临问题。 UPD3:更新日志cat


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

你不能把
findViewById(R.id.xbox)放进去在你的类主体中。它应该在
setContentView()之后转到
onCreate()

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

Sensor Sensor=sm.getDefaultSensor(Sensor.TYPE\u加速计)

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

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

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

你不能把
findviewbyd(R.id.xbox)放进去在你的类主体中。它应该在
setContentView()之后转到
onCreate()

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

Sensor Sensor=sm.getDefaultSensor(Sensor.TYPE\u加速计)

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

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

 public SensorActivity() {
     sm = (SensorManager)getSystemService(SENSOR_SERVICE);
     sensor = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
 }
在onCreate()中,尝试从所有6条findViewById语句中删除开头的TextView

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

编辑:

在onCreate()中,尝试从所有6条findViewById语句中删除开头的TextView

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

编辑:



非常感谢你的回答,我非常感激。不幸的是,它仍然不起作用。我更新了logcat。你仍然没有初始化你的SensorManager。请参阅更新的答案。当我放置sm=new SensonManager()时,它会给我错误;在onCreate下,logcat中不再有21行,当我移动texviewsYeah后,我不知道如何正确初始化它,因为我从未使用过SensorManager,但检查了文档并再次更新了答案;)上帝啊,你真聪明。我现在真的应该出去放松一下,我是如何度过的。谢谢,真的。非常感谢你的回答,我非常感激。不幸的是,它仍然不起作用。我更新了logcat。你仍然没有初始化你的SensorManager。请参阅更新的答案。当我放置sm=new SensonManager()时,它会给我错误;在onCreate下,logcat中不再有21行,当我移动texviewsYeah后,我不知道如何正确初始化它,因为我从未使用过SensorManager,但检查了文档并再次更新了答案;)上帝啊,你真聪明。我现在真的应该出去放松一下,我是如何度过的。谢谢,真的。现在可以了。现在上传你最新的日志。还有,你的线路是什么?18?它不起作用。我更新了日志。我的18号线是专用传感器;我认为你需要仔细阅读日志。在onCreate()中调用super,如下所示:super.onCreate(savedInstanceState);是的,没错。这是我通过的。谢谢。现在上传你最新的日志。还有,你的线路是什么?18?它不工作。我更新了日志。我的18号线是专用传感器;我认为你需要仔细阅读日志。在onCreate()中调用super,如下所示:super.onCreate(savedInstanceState);是的,没错。这就是我通过的。谢谢。
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);

}
..