0

直到今天,我的 SensorManager 都运行良好。我没有更改任何代码,我所做的只是启动 eclipse,现在我收到一个错误,迫使我的应用程序退出;这是我得到的:

02-20 11:44:50.835: E/AndroidRuntime(23828): FATAL EXCEPTION: main
02-20 11:44:50.835: E/AndroidRuntime(23828): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.synlight_development.sleep_smart/com.synlight_development.sleep_smart.Sleep}: java.lang.NullPointerException
02-20 11:44:50.835: E/AndroidRuntime(23828):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2088)
02-20 11:44:50.835: E/AndroidRuntime(23828):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2113)
02-20 11:44:50.835: E/AndroidRuntime(23828):    at android.app.ActivityThread.access$700(ActivityThread.java:139)
02-20 11:44:50.835: E/AndroidRuntime(23828):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1224)
02-20 11:44:50.835: E/AndroidRuntime(23828):    at android.os.Handler.dispatchMessage(Handler.java:99)
02-20 11:44:50.835: E/AndroidRuntime(23828):    at android.os.Looper.loop(Looper.java:137)
02-20 11:44:50.835: E/AndroidRuntime(23828):    at android.app.ActivityThread.main(ActivityThread.java:4918)
02-20 11:44:50.835: E/AndroidRuntime(23828):    at java.lang.reflect.Method.invokeNative(Native Method)
02-20 11:44:50.835: E/AndroidRuntime(23828):    at java.lang.reflect.Method.invoke(Method.java:511)
02-20 11:44:50.835: E/AndroidRuntime(23828):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1004)
02-20 11:44:50.835: E/AndroidRuntime(23828):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:771)
02-20 11:44:50.835: E/AndroidRuntime(23828):    at dalvik.system.NativeStart.main(Native Method)
02-20 11:44:50.835: E/AndroidRuntime(23828): Caused by: java.lang.NullPointerException
02-20 11:44:50.835: E/AndroidRuntime(23828):    at android.content.ContextWrapper.getSystemService(ContextWrapper.java:423)
02-20 11:44:50.835: E/AndroidRuntime(23828):    at com.synlight_development.sleep_smart.Sleep$Accelerator.<init>(Sleep.java:173)
02-20 11:44:50.835: E/AndroidRuntime(23828):    at com.synlight_development.sleep_smart.Sleep.onCreate(Sleep.java:104)
02-20 11:44:50.835: E/AndroidRuntime(23828):    at android.app.Activity.performCreate(Activity.java:5048)
02-20 11:44:50.835: E/AndroidRuntime(23828):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
02-20 11:44:50.835: E/AndroidRuntime(23828):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2052)
02-20 11:44:50.835: E/AndroidRuntime(23828):    ... 11 more

这是我的传感器类:

public class Accelerator extends Service implements SensorEventListener
    {
        public Accelerator()
        {
            SensorManager sm = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
            if(sm.getSensorList(Sensor.TYPE_GYROSCOPE).size() != 0)
            {
                Sensor s = sm.getSensorList(Sensor.TYPE_GYROSCOPE).get(0);
                sm.registerListener(this, s, SensorManager.SENSOR_DELAY_GAME);
            }
        }
        public void onAccuracyChanged(Sensor s, int i) 
        {

        }
}
4

1 回答 1

1

移动:

SensorManager sm = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
if(sm.getSensorList(Sensor.TYPE_GYROSCOPE).size() != 0)
{
    Sensor s = sm.getSensorList(Sensor.TYPE_GYROSCOPE).get(0);
    sm.registerListener(this, s, SensorManager.SENSOR_DELAY_GAME);
}

进入你的服务的 onCreate()。getSystemService()是 Context 中的一个方法,Service 对其进行了扩展。但是,在调用 onCreate() 之前,您没有上下文。因此,这会导致构造函数中出现 NPE。

此外,请确保您使用 startService() 启动服务,而不是从中创建普通 Java 对象

于 2013-02-20T16:56:55.323 回答