错误java.lang.NullPointerException:尝试调用虚方法' java.lang.Object android.content.Context.getSystemService

时间:2016-11-21 19:01:04

标签: java android nullpointerexception sensor

我正在创建一个简单的Android类来更新一个变量,该变量具有从某些特定传感器(Light,temp等)获取的最新值,我通过getData()方法访问它(传感器值),但是我收到了错误。我希望你能帮助我!

这是我的通用传感器类。

MySensor

public class mySensor extends Service implements SensorEventListener {

private SensorManager mSensorManager =null;
private Sensor s=null;

float data = 0;


public void onCreate(){
    super.onCreate();
    mSensorManager= (SensorManager) getSystemService(Context.SENSOR_SERVICE);


}


public mySensor(int type){

    //HERE I'M GETTING THE ERROR
    s= mSensorManager.getDefaultSensor(type);




}

public String getData(){

    return String.valueOf(data);
}

@Override
public IBinder onBind(Intent intent) {
    return null;
}

@Override
public void onSensorChanged(SensorEvent event) {
    if (event.sensor.getType() == Sensor.TYPE_LIGHT) {
        data = event.values[0];
    }

}

@Override
public void onAccuracyChanged(Sensor sensor, int i) {

}
protected void onResume() {

    mSensorManager.registerListener((SensorEventListener) this, s, SensorManager.SENSOR_DELAY_NORMAL);


}

protected void onPause() {
   // super.onPause();
    mSensorManager.unregisterListener((SensorEventListener) this);
}
}

这是日志

 ...
E/AndroidRuntime: FATAL EXCEPTION: main
                  Process: project.luke.com.sensorreader, PID: 25297
                  java.lang.RuntimeException: Unable to start activity ComponentInfo{project.luke.com.sensorreader/project.luke.com.sensorreader.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.hardware.Sensor android.hardware.SensorManager.getDefaultSensor(int)' on a null object reference
                      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2460)
                      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2522)
                      at android.app.ActivityThread.access$800(ActivityThread.java:169)
                      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1421)
                      at android.os.Handler.dispatchMessage(Handler.java:111)
                      at android.os.Looper.loop(Looper.java:194)
                      at android.app.ActivityThread.main(ActivityThread.java:5546)
                      at java.lang.reflect.Method.invoke(Native Method)
                      at java.lang.reflect.Method.invoke(Method.java:372)
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:967)
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:762)
                   Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.hardware.Sensor android.hardware.SensorManager.getDefaultSensor(int)' on a null object reference
                      at project.luke.com.sensorreader.mySensor.<init>(mySensor.java:39)
                      at project.luke.com.sensorreader.MainActivity.onCreate(MainActivity.java:32)
                      at android.app.Activity.performCreate(Activity.java:5975)
                      at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1111)
                      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2413)
                      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2522) 
                      at android.app.ActivityThread.access$800(ActivityThread.java:169) 
                      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1421) 
                      at android.os.Handler.dispatchMessage(Handler.java:111) 
                      at android.os.Looper.loop(Looper.java:194) 
                      at android.app.ActivityThread.main(ActivityThread.java:5546) 
                      at java.lang.reflect.Method.invoke(Native Method) 
                      at java.lang.reflect.Method.invoke(Method.java:372) 
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:967) 
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:762) 

在我的MainActivity类中,我简单地调用mLight = new mySensor(Sensor.TYPE_LIGHT);但这会触发错误,导致mSensorManager = null,总是如此。

1 个答案:

答案 0 :(得分:0)

从您的MainActivity:

Intent intent = new Intent(this, mySensor.class);
intent.putExtra("type", Sensor.TYPE_LIGHT);

并在您的服务范围内:

@Override
public IBinder onBind(Intent intent) {
    mSensorManager= (SensorManager) getSystemService(Context.SENSOR_SERVICE);
    int type = intent.getExtras().get("type");
    s= mSensorManager.getDefaultSensor(type);
    return null;
}

然后你可以删除:

public mySensor(int type){
   ...
}

并最终在onCreate内编写的代码。