机器人:光传感器的NullPointerException [英] android: light sensor nullPointerException

查看:268
本文介绍了机器人:光传感器的NullPointerException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的活动我都堪称一TextView的senzosv。我想让它显示从我的光传感器的电流值。我有以下code。

In my activity I have one TextView called "senzosv". I want it to show current value from my light sensor. I have following code.

public class Osvetlenie extends Activity implements OnItemSelectedListener, OnSeekBarChangeListener, SensorEventListener  {


SensorManager sm;
Sensor proxSensor;
Sensor lighhtSens;
TextView tv = (TextView)findViewById(R.id.senzosv);

这是我的onCreate:

This is in my 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

And here are methods onSensorChanged and 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)的应用程序
我怎么错了?谢谢:)

When I run my application, and when I am trying to start this activity, I get nullpointexception in LogCat and error message: application unexpectedly closed I am testing my app on Samsung Galaxy S II (GT-I9100) What did I wrong? Thank you :)

推荐答案

做到这一点。

tv = (TextView)findViewById(R.id.senzosv);

的setContentView()

这篇关于机器人:光传感器的NullPointerException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆