在我的活动中,我有一个名为“senzosv”的TextView。我希望它能显示我的光线传感器的当前值。我有以下代码。android:光传感器nullPointerException
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,错误消息:应用程序意外关闭 我在Samsung Galaxy S II(GT-I9100)上测试我的应用程序 我错了什么?谢谢:)
谢谢。它现在运作良好。我还有一个问题。光线传感器能够仅返回值1000,100和10吗?或者它可以更准确吗?谢谢 – 2013-03-10 11:03:34
如果这解决了你的问题接受回答 – DjHacktorReborn 2013-03-10 11:04:22