-2

我想使用摇动命令导航页面,但它出现错误,所以我在代码中遗漏了什么。我不明白什么是加速度计。这是我的代码。

public class ACTIVITY extends Activity { /** 首次创建活动时调用。*/

        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);

            SensorManager mSensorManager;

            ShakeEvent mSensorListener;

            mSensorListener = new ShakeEvent();
            mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
            mSensorManager.registerListener(mSensorListener,
                mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
                SensorManager.SENSOR_DELAY_UI);


            mSensorListener.setOnShakeListener(new ShakeEvent.OnShakeListener() {

              public void onShake() {
                  Intent i = new Intent(shake.this, NEWACTIVITY.class);
                  startActivity(i);
              }
            });
        }}

感谢您的帮助。

4

1 回答 1

0

You said that your activity is called "ACTIVITY". But in your onShake method you create an Intent with first argument "shake.this". That doesn't make any sense, because your OnShakeListener is not nested in a class called "shake" (and furthermore that first argument must be a Context object!). You need to write the following instead:

          public void onShake() {
              Intent i = new Intent(ACTIVITY.this, NEWACTIVITY.class);
              startActivity(i);
          }
于 2012-07-15T06:08:04.983 回答