使用Accelerometer访问活动

时间:2014-07-24 22:36:14

标签: android-intent android-activity accelerometer

我试图在2天内制作这段代码我是Android编码的初学者。 我需要使用Accelerometer访问我的应用程序内的另一个活动。想法是在用户摇动设备时打开其他活动。

我试图使用此代码..

import android.app.Activity;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.hardware.Sensor;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

import com.example.cyrculos.ShakeDetector.OnShakeListener;



public class EnglishActivity extends Activity {

    private SensorManager mSensorManager;
    private Sensor mAccelerometer;
    private ShakeDetector mShakeDetector;


        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_english);

            mSensorManager =(SensorManager) getSystemService(SENSOR_SERVICE);
            mAccelerometer = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
            mShakeDetector = new ShakeDetector (new OnShakeListener() {

                @Override
                public void onShake() {

                    Intent intent = new Intent( null, AnimationActivity.class);
                    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    startActivity(intent);                                                                                                                                                                                                                                                                      

                }
            });


        }

        @Override
        public void onResume() {
            super.onResume();
            mSensorManager.registerListener(mShakeDetector, mAccelerometer, 
                    SensorManager.SENSOR_DELAY_UI);
        }

        @Override
        public void onPause() {
            super.onPause();
            mSensorManager.unregisterListener(mShakeDetector);

        }
}





     07-25 19:57:56.645: I/ApplicationPackageManager(30912): cscCountry is not German : ZTO
        07-25 19:57:56.809: D/dalvikvm(30912): GC_EXTERNAL_ALLOC freed 68K, 47% free 2864K/5379K, external 688K/1036K, paused 59ms
        07-25 19:57:58.746: I/ApplicationPackageManager(30912): cscCountry is not German : ZTO
        07-25 19:57:58.793: D/dalvikvm(30912): GC_EXTERNAL_ALLOC freed 14K, 47% free 2894K/5379K, external 1830K/2004K, paused 42ms
        07-25 19:57:58.949: D/SensorManager(30912): ====>>>>>Num Sensor: 1
        07-25 19:57:58.949: D/SensorManager(30912): ====>>>>>Num Sensor: 2
        07-25 19:57:58.949: D/SensorManager(30912): ====>>>>>Num Sensor: 3
        07-25 19:57:58.949: D/SensorManager(30912): ====>>>>>Num Sensor: 4
        07-25 19:57:58.949: D/SensorManager(30912): ====>>>>>Num Sensor: 5
        07-25 19:57:58.949: D/SensorManager(30912): ====>>>>>Num Sensor: 6
        07-25 19:57:58.949: D/SensorManager(30912): ====>>>>>Num Sensor: 0
        07-25 19:57:58.965: E/SensorManager(30912): registerListener :: handle = 1  name= BMA222 Acceleration Sensor delay= 60000 Listener= com.example.cyrculos.ShakeDetector@40522d90
        07-25 19:57:58.973: E/SensorManager(30912): =======>>>Sensor Thread RUNNING <<<========
        07-25 19:57:59.254: E/SensorManager(30912): reg :: handle = 1
        07-25 19:58:12.965: W/dalvikvm(30912): threadid=1: thread exiting with uncaught exception (group=0x4001c578)
        07-25 19:58:12.981: E/AndroidRuntime(30912): FATAL EXCEPTION: main
        07-25 19:58:12.981: E/AndroidRuntime(30912): java.lang.NullPointerException
        07-25 19:58:12.981: E/AndroidRuntime(30912):    at android.content.ComponentName.<init>(ComponentName.java:75)
        07-25 19:58:12.981: E/AndroidRuntime(30912):    at android.content.Intent.<init>(Intent.java:2823)
        07-25 19:58:12.981: E/AndroidRuntime(30912):    at com.example.cyrculos.EnglishActivity$1.onShake(EnglishActivity.java:31)
        07-25 19:58:12.981: E/AndroidRuntime(30912):    at com.example.cyrculos.ShakeDetector.onSensorChanged(ShakeDetector.java:74)
        07-25 19:58:12.981: E/AndroidRuntime(30912):    at android.hardware.SensorManager$ListenerDelegate$1.handleMessage(SensorManager.java:538)
        07-25 19:58:12.981: E/AndroidRuntime(30912):    at android.os.Handler.dispatchMessage(Handler.java:99)
        07-25 19:58:12.981: E/AndroidRuntime(30912):    at android.os.Looper.loop(Looper.java:130)
        07-25 19:58:12.981: E/AndroidRuntime(30912):    at android.app.ActivityThread.main(ActivityThread.java:3687)
        07-25 19:58:12.981: E/AndroidRuntime(30912):    at java.lang.reflect.Method.invokeNative(Native Method)
        07-25 19:58:12.981: E/AndroidRuntime(30912):    at java.lang.reflect.Method.invoke(Method.java:507)
        07-25 19:58:12.981: E/AndroidRuntime(30912):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
        07-25 19:58:12.981: E/AndroidRuntime(30912):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
        07-25 19:58:12.981: E/AndroidRuntime(30912):    at dalvik.system.NativeStart.main(Native Method)

0 个答案:

没有答案