应用程序在SensorManager上崩溃

时间:2012-05-02 01:39:02

标签: android sensormanager

我正在开发一个Android应用程序,我想在其中使用加速度计。 但每当我打电话给这个方法时:

SensorManager sm = (SensorManager)context.getSystemService(Context.SEARCH_SERVICE);

我的应用程序崩溃了。

我的代码是:

    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    context = getApplicationContext();
    SensorManager sm = (SensorManager)context.getSystemService(Context.SEARCH_SERVICE);
    if(sm.getSensorList(Sensor.TYPE_ACCELEROMETER).size()!=0)
    {

        Sensor s = sm.getSensorList(Sensor.TYPE_ACCELEROMETER).get(0);
        sm.registerListener(this, s, SensorManager.SENSOR_DELAY_NORMAL);
    }

    x = y = sensorX = sensorY = 0;

}

我在网上搜索但无法找到任何解决方案。我正在手机上调试我的应用程序而不是在模拟器上。在这方面请帮助我,为什么它会崩溃,我该如何解决它。

以下是日志cat错误: 05-02 06:40:04.299:W / dalvikvm(9525):threadid = 1:线程退出未捕获异常(组= 0x40018578)

   05-02 06:40:04.309: E/AndroidRuntime(9525): FATAL EXCEPTION: main
   05-02 06:40:04.309: E/AndroidRuntime(9525): java.lang.RuntimeException: Unable to     start activity ComponentInfo{com.accelerometer/com.accelerometer.AccelerometerActivity}: java.lang.ClassCastException: android.app.SearchManager

   05-02 06:40:04.309: E/AndroidRuntime(9525):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1651)

  05-02 06:40:04.309: E/AndroidRuntime(9525):   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)

  05-02 06:40:04.309: E/AndroidRuntime(9525):   at android.app.ActivityThread.access$1500(ActivityThread.java:117)

  05-02 06:40:04.309: E/AndroidRuntime(9525):   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)

  05-02 06:40:04.309: E/AndroidRuntime(9525):   at android.os.Handler.dispatchMessage(Handler.java:99)

  05-02 06:40:04.309: E/AndroidRuntime(9525):   at android.os.Looper.loop(Looper.java:123)

  05-02 06:40:04.309: E/AndroidRuntime(9525):   at android.app.ActivityThread.main(ActivityThread.java:3687)

 05-02 06:40:04.309: E/AndroidRuntime(9525):    at java.lang.reflect.Method.invokeNative(Native Method)

 05-02 06:40:04.309: E/AndroidRuntime(9525):    at java.lang.reflect.Method.invoke(Method.java:507)

 05-02 06:40:04.309: E/AndroidRuntime(9525):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)

 05-02 06:40:04.309: E/AndroidRuntime(9525):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)

 05-02 06:40:04.309: E/AndroidRuntime(9525):    at dalvik.system.NativeStart.main(Native Method)

 05-02 06:40:04.309: E/AndroidRuntime(9525): Caused by: java.lang.ClassCastException: android.app.SearchManager

 05-02 06:40:04.309: E/AndroidRuntime(9525):    at com.accelerometer.AccelerometerActivity.onCreate(AccelerometerActivity.java:24)

05-02 06:40:04.309: E/AndroidRuntime(9525):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)

05-02 06:40:04.309: E/AndroidRuntime(9525):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615)

感谢。

1 个答案:

答案 0 :(得分:0)

SensorManager sm = (SensorManager)context.getSystemService(Context.SEARCH_SERVICE);

这将从getSystemService()返回一个SearchManager,您无法将SearchManager转换为SensorManager,关闭但不完全相同......

要获取SensorManager,请尝试以下操作:(取自Dev Guide

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