我正在開發一個我想使用加速計的android應用程序。 但是,每當我打電話給我的方法:SensorManager上的應用程序崩潰
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;
}
我做了網站,但無法找到任何解決方案的搜索。我正在手機上調試我的應用程序,而不是在仿真器上。請在這方面幫助我,爲什麼它崩潰,我該如何解決它。
以下是日誌貓錯誤: 6月5日至2日:40:04.299:W/dalvikvm(9525):線程ID = 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)
由於離開。
請發佈任何logcat錯誤。 – Sam
我編輯了這篇文章,併發布了我的logcat錯誤。 –