0

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

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

} 

升级版:

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:我更新了代码,现在它可以工作了。也许有人会需要它。谢谢。

4

2 回答 2

3

你不能把findViewById(R.id.xbox);你的班级身体。它应该去onCreate()after setContentView()

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

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

因为你写: SensorManager sm; 所以传感器管理器是空的,所以你不能在sm.getDefaultSensor().

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

 public SensorActivity() {
     sm = (SensorManager)getSystemService(SENSOR_SERVICE);
     sensor = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
 }
于 2012-05-18T13:30:30.293 回答
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);

}
..
于 2012-05-18T14:15:02.727 回答