I try to implement the example code from https://developer.android.com/guide/topics/sensors/sensors_overview.html -> "Monitoring Sensor Events"
I want to write an example app which reads the sensor input from the light-sensor and displays it in a textview.
The activity_main.xml countains a single TextView with the id "@+id/textview".
This is my code
package com.example.asd.sensor2;
import android.app.Activity;
import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.widget.TextView;
public class MainActivity extends Activity implements SensorEventListener {
SensorManager sensorManager;
Sensor light;
TextView textView;
@override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = (TextView) findViewById(R.id.textview);
sensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
light = sensorManager.getDefaultSensor(Sensor.TYPE_LIGHT);
}
@override
public void onSensorChanged(SensorEvent sensorEvent) {
float lux = sensorEvent.values[0];
//textView.setText((int) lux);
}
@override
protected void onResume() {
super.onResume();
sensorManager.registerListener(this, light, SensorManager.SENSOR_DELAY_NORMAL);
}
@override
protected void onPause() {
super.onPause();
sensorManager.unregisterListener(this);
}
@override
public void onAccuracyChanged(Sensor sensor, int i) {
}
}
It works but if I uncomment the textView.setText((int) lux); the app crashes.
I want to write an example app which reads the sensor input from the light-sensor and displays it in a textview.
The activity_main.xml countains a single TextView with the id "@+id/textview".
This is my code
package com.example.asd.sensor2;
import android.app.Activity;
import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.widget.TextView;
public class MainActivity extends Activity implements SensorEventListener {
SensorManager sensorManager;
Sensor light;
TextView textView;
@override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = (TextView) findViewById(R.id.textview);
sensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
light = sensorManager.getDefaultSensor(Sensor.TYPE_LIGHT);
}
@override
public void onSensorChanged(SensorEvent sensorEvent) {
float lux = sensorEvent.values[0];
//textView.setText((int) lux);
}
@override
protected void onResume() {
super.onResume();
sensorManager.registerListener(this, light, SensorManager.SENSOR_DELAY_NORMAL);
}
@override
protected void onPause() {
super.onPause();
sensorManager.unregisterListener(this);
}
@override
public void onAccuracyChanged(Sensor sensor, int i) {
}
}
It works but if I uncomment the textView.setText((int) lux); the app crashes.