调用方法setRequestedOrientation()时出错

时间:2010-08-18 17:48:40

标签: android

public void onSensorChanged(SensorEvent e) {
    if (e.sensor.getType()==Sensor.TYPE_ACCELEROMETER) {

        if(e.values[0] >= 8)
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
        else
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
    }
}

当调用方法时setRequestedOrientation() - 错误。创建layout-land并在其中放置main.xml文件的副本。

1 个答案:

答案 0 :(得分:0)

试试这个::

SensorManager sensorMgr;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    sensorMgr = (SensorManager) getSystemService(SENSOR_SERVICE);
    sensorMgr.registerListener(this,
            SensorManager.SENSOR_ACCELEROMETER,
            SensorManager.SENSOR_DELAY_GAME);
}


public void onSensorChanged(int sensor, float[] values){
    if (sensor==Sensor.TYPE_ACCELEROMETER) {
        if(values[0] >= 8)
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
        else
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
    }
}