在我的活动中,我有一个名为“senzosv”的 TextView。我希望它显示我的光传感器的当前值。我有以下代码。
public class Osvetlenie extends Activity implements OnItemSelectedListener, OnSeekBarChangeListener, SensorEventListener {
SensorManager sm;
Sensor proxSensor;
Sensor lighhtSens;
TextView tv = (TextView)findViewById(R.id.senzosv);
这是在我的 onCreate 中:
...
sm = (SensorManager) getSystemService(SENSOR_SERVICE);
proxSensor =sm.getDefaultSensor(Sensor.TYPE_PROXIMITY);
lighhtSens = sm.getDefaultSensor(Sensor.TYPE_LIGHT);
sm.registerListener(this, proxSensor, SensorManager.SENSOR_DELAY_NORMAL);
sm.registerListener(this, lighhtSens, SensorManager.SENSOR_DELAY_NORMAL);
这里是 onSensorChanged 和 onAccuracyChanged 方法
public void onSensorChanged(SensorEvent event)
{
if( event.sensor.getType() == Sensor.TYPE_LIGHT)
{
tv.setText("value: " + event.values[0] + " lux" );
Toast.makeText(getApplicationContext(),"On SensorChanged"+ event.values[0],Toast.LENGTH_SHORT).show();
}
}
public void onAccuracyChanged(Sensor sensor, int accuracy) {
Log.d(ACCESSIBILITY_SERVICE,"onAccuracyChanged: " + sensor + ", accuracy: " + accuracy);
if(sensor.getType() == Sensor.TYPE_LIGHT){
Log.i("Sensor Changed", "Accuracy :" + accuracy);
}
}
当我运行我的应用程序并尝试启动此活动时,我在 LogCat 中收到 nullpointexception 和错误消息:应用程序意外关闭 我正在三星 Galaxy S II (GT-I9100) 上测试我的应用程序我做错了什么?谢谢 :)