코드에 문제가 있습니다. 두 가지 클래스가 있습니다 : MainActivity 센서 (이 경우에는 광 센서)를 사용할 수 있는지 그리고 예인 경우 다른 클래스의 LightSensor에서 센서로 날짜를 가져 오려고하지만 결과는 항상 null입니다. 나는 내가 청취자와 함께 뭔가 잘못하고 있다고 생각하지만, 나는 무엇을 해야할지 모르겠다. 나는 몇 시간 동안 앉아 있었고 아직도 아무것도하지 않았다. 어떤 생각이 든다면, 글을 쓰고 나를 도와주세요.별도의 클래스에서 센서의 데이터를 읽는 방법
`public class MainActivity extends Activity implements EventListener {
SensorManager mSensorManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView wyswietl = (TextView)findViewById(R.id.res);
mSensorManager = (SensorManager)getSystemService(SENSOR_SERVICE);
LightSensor mLightSensor = new LightSensor(getBaseContext());
if (mSensorManager.getDefaultSensor(Sensor.TYPE_LIGHT) != null){
//mLightSensor.register();
String newLux = mLightSensor.getLux();
wyswietl.setText("Light level: " + newLux);
}
else{
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}`
및 MainActivioty 클래스에 내가 그것을 생성자 인수 모른다 :
MainActivity 클래스
LightSensor mLightSensor = new LightSensor(getBaseContext());
좋은 ...
LightSensor 클래스 :
`public class LightSensor implements SensorEventListener {
public SensorManager mSensorManagerx;
public Sensor lightManager;
public String lux;
Context context;
public LightSensor(Context context){
//public void onCreateLight(Context context){
this.context = context;
mSensorManagerx = (SensorManager)context.getSystemService(Context.SENSOR_SERVICE);
lightManager = mSensorManagerx.getDefaultSensor(Sensor.TYPE_LIGHT);
}
public void register(){
mSensorManagerx.registerListener(this, lightManager, SensorManager.SENSOR_DELAY_NORMAL);
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
// TODO Auto-generated method stub
}
@Override
public void onSensorChanged(SensorEvent event) {
// TODO Auto-generated method stub
lux = Float.toString(event.values[0]);
}
public String getLux(){
return lux;
}
public void unregister(){
mSensorManagerx.unregisterListener(this);
}
}`
당신이 시스템을'onSensorChanged'를 호출 할 수있는 기회를 제공하지 않습니다. 당신은'register'를 호출하지 않고'lux'가 설정되어 있는지 확인하지 않습니다. – njzk2