在我的项目中收到错误:
02-20 23:32:45.433: E/Receiver(19076): Read: Error
02-20 23:32:45.433: E/Receiver(19076): java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
02-20 23:32:45.433: E/Receiver(19076): at android.os.Handler.<init>(Handler.java:200)
02-20 23:32:45.433: E/Receiver(19076): at android.os.Handler.<init>(Handler.java:114)
02-20 23:32:45.433: E/Receiver(19076): at android.app.Activity.<init>(Activity.java:793)
02-20 23:32:45.433: E/Receiver(19076): at stas.tcp.client.SensorsActivity.<init>(SensorsActivity.java:29)
02-20 23:32:45.433: E/Receiver(19076): at stas.tcp.client.SPN_API.invoke_command(SPN_API.java:208)
02-20 23:32:45.433: E/Receiver(19076): at stas.tcp.client.SPN_engine.doInvokeCommand(SPN_engine.java:164)
02-20 23:32:45.433: E/Receiver(19076): at stas.tcp.client.SPN_engine.processPoUART(SPN_engine.java:127)
02-20 23:32:45.433: E/Receiver(19076): at stas.tcp.client.PoUARTSupport.checkPacket(PoUARTSupport.java:65)
02-20 23:32:45.433: E/Receiver(19076): at stas.tcp.client.Receiver.run(Receiver.java:46)
02-20 23:32:45.433: E/Receiver(19076): at java.lang.Thread.run(Thread.java:841)
我不知道这对我意味着什么。我的项目中有很少的主题,而且我不知道哪个错误。当我在SensorsActivity.class中注释掉一个并且只有thred时 - 错误仍然存在。
- EDIT ---
public class SensorsActivity extends Activity implements EventListener, StepCounterListener, LinearAccListener, StepDetectorListener, LightSensorListener, PressureSensorListener, GravitySensorListener, RotationVectorListener, RelativeHumidityListener, ProximitySensorListener, TempSensorListener, AccelerometerSensorListener, GyroSensorListener, MagSensorListener {
public static final long axisssx = 0;
private TextView lightText, pressureText, signText, noiseText, humidityText, linearAccText, proximityText, tempText, accelText, gyroText, magText, rotationText, gravityText, stepDetectorText, stepCounterText;
private LightSensor mLightSensor;
private PressureSensor mPressureSensor;
private RelativeHumiditySensor mRelativeHumidity;
private ProximitySensor mProximitySensor;
private TemperatureSensor mTempSensor;
private AccelerometerSensor mAccelSensor;
private GyroscopeSensor mGyroSensor;
private MagnetometerSensor mMagSensor;
private RotationSensor mRotSensor;
private GravitySensor mGravitySensor;
private StepCounterSensor mStepCounter;
private StepDetectorSensor mStepDetector;
private LinearAccelerometer mLinearAcc;
private NoiseLevel mNoise;
public double amp;
private Handler handler1;
public long accelx, accely, accelz;
private SPN_native spnNative;
private SPN_API spnApi;
private int argsImageLength;
private int actualNrOfArgs;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.sensors);
lightText = (TextView)findViewById(R.id.rLight);
pressureText = (TextView)findViewById(R.id.rPressure);
humidityText = (TextView)findViewById(R.id.rHumidity);
proximityText = (TextView)findViewById(R.id.rProximity);
tempText = (TextView)findViewById(R.id.rTemp);
accelText = (TextView)findViewById(R.id.rAccel);
gyroText = (TextView)findViewById(R.id.rGyro);
magText = (TextView)findViewById(R.id.rMag);
rotationText = (TextView)findViewById(R.id.rRotation);
gravityText = (TextView)findViewById(R.id.rGravity);
stepCounterText = (TextView)findViewById(R.id.rSCounter);
stepDetectorText = (TextView)findViewById(R.id.rSDetector);
linearAccText = (TextView)findViewById(R.id.rLinearAcc);
signText = (TextView)findViewById(R.id.rSignMotion);
noiseText = (TextView)findViewById(R.id.rNoise);
//SOUND LEVEL CODE:
mNoise = new NoiseLevel();
try {
mNoise.starter();
//Toast.makeText(getBaseContext(), "Sound sensor initiated.", Toast.LENGTH_SHORT).show();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
handler1 = new Handler();
final Runnable r = new Runnable() {
//this.runOnUiThread(new Runnable(){
public void run() {
Toast.makeText(getBaseContext(), "Working!", Toast.LENGTH_LONG).show();
runOnUiThread(new Runnable() {
@Override
public void run() {
// Get the volume from 0 to 255 in 'int'
//double volume = 10 * mNoise.getTheAmplitude() / 32768;
double volume = mNoise.getTheAmplitude();
int volumeToSend = (int) volume;
noiseText.setText("Volume: " + String.valueOf(volumeToSend));
handler1.postDelayed(this, 250); // amount of delay between every cycle of volume level detection + sending the data out
}
});
}
};
handler1.postDelayed(r, 250);
mLightSensor = new LightSensor(getBaseContext());
mLightSensor.setListener(this);
Log.e("Sensor Activity:" , "LIGHT SENSOR LISTENER ACTIVE");
mPressureSensor = new PressureSensor(getBaseContext());
mPressureSensor.setListener(this);
Log.e("Sensor Activity:" , "PRESSURE SENSOR LISTENER ACTIVE");
mRelativeHumidity = new RelativeHumiditySensor(getBaseContext());
mRelativeHumidity.setListener(this);
mProximitySensor = new ProximitySensor(getBaseContext());
mProximitySensor.setListener(this);
Log.e("Sensor Activity:" , "PROXIMITY SENSOR LISTENER ACTIVE");
mTempSensor = new TemperatureSensor(getBaseContext());
mTempSensor.setListener((TempSensorListener) this);
mAccelSensor = new AccelerometerSensor(getBaseContext());
mAccelSensor.setListener(this);
mGyroSensor = new GyroscopeSensor(getBaseContext());
mGyroSensor.setListener(this);
mMagSensor = new MagnetometerSensor(getBaseContext());
mMagSensor.setListener(this);
mRotSensor = new RotationSensor(getBaseContext());
mRotSensor.setListener(this);
mGravitySensor = new GravitySensor(getBaseContext());
mGravitySensor.setListener(this);
mStepCounter = new StepCounterSensor(getBaseContext());
mStepCounter.setListener(this);
mStepDetector = new StepDetectorSensor(getBaseContext());
mStepDetector.setListener(this);
mLinearAcc = new LinearAccelerometer(getBaseContext());
mLinearAcc.setListener(this);
spnNative = new SPN_native();
spnApi = new SPN_API();
}
public void onLightSensorChanged(int lux){
lightText.setText("LIGHT level: " + lux + " lx");
}
public void onPressureSensorChanged(long pressure){
pressureText.setText("\nPRESSURE: " + pressure + " mPa ");
}
public void onRelativeHumidityChanged(String humidity) {
humidityText.setText("\nRELATIVE HUMIDITY: " + humidity);
}
public void onProximitySensorChanged(int proximity) {
proximityText.setText("\nPROXIMITY: " + proximity);
}
public void onTempSensorChanged(String temperature) {
tempText.setText("\nAMBIENT TEMPERATURE :" + temperature + " celcius degree");
}
public void onAccelSensorChanged(long axisX, long axisY, long axisZ) {
accelText.setText("\nACCELEROMETER: \nX: " + axisX + "nm/s^2 || Y: " + axisY + "nm/s^2 || Z: " + axisZ +"nm/s^2");
}
public void onGyroSensorChanged(long axisX, long axisY, long axisZ) {
gyroText.setText("\nGYROSCOPE: \nX: " + axisX + "nrad/s || Y: " + axisY + "nrad/s || Z: " + axisZ + "nrad/s");
}
public void onMagSensorChanged(long axisX, long axisY, long axisZ) {
magText.setText("\nMAGNETOMETER: \nX: " + axisX + " piko Tesla || Y: " + axisY + " piko Tesla || Z: " + axisZ + " piko Tesla");
}
public void onRotationVectorChanged(long rot1, long rot2, long rot3) {
rotationText.setText("\nROTATION VECTOR: \nYaw: " + rot1 + " nrad || Pitch: " + rot2 + " nrad || Roll: " + rot3 + " nrad");
}
public void onGravitySensorChanged(long gravity1, long gravity2, long gravity3) {
gravityText.setText("\nGRAVITY: \nX: " + gravity1 + "nm/s^2 || Y: " + gravity2 + "nm/s^2 || Z: " + gravity3 + "nm/s^2");
}
public void onStepCounterChanged(int step) {
stepCounterText.setText("\nSTEP CONTER: " + step + " steps");
}
public void onStepDetectorChanged(String step) {
stepDetectorText.setText("\nSTEP DETECTOR: " + step);
}
public void onLinearAccChanged(long axisX, long axisY, long axisZ) {
linearAccText.setText("\nLINEAR ACCELERATION: \nX: " + axisX + "nm/s^2 || Y: " + axisY + "nm/s^2 || Z: " + axisZ + "nm/s^2");
}
@Override
protected void onResume() {
super.onStart();
// Sound based code
// try {
// mNoise = new NoiseLevel();
// mNoise.starter();
// Toast.makeText(getBaseContext(), "Sound sensor initiated.", Toast.LENGTH_SHORT).show();
// } catch (IllegalStateException e) {
// // TODO Auto-generated catch block
// Toast.makeText(getBaseContext(), "On resume, sound sensor messed up...", Toast.LENGTH_LONG).show();
// e.printStackTrace();
// }
if(!mLightSensor.register()){
lightText.setText("\nLight sensor not supported!");
// Toast.makeText(this, "Light sensor not supported!", Toast.LENGTH_SHORT).show();
}
if(!mPressureSensor.register()){
pressureText.setText("\nPressure sensor not supported!");
}
if(!mRelativeHumidity.register()){
humidityText.setText("\nRelative humidity sensor not supported!");
}
if(!mProximitySensor.register()){
proximityText.setText("\nProximity sensor not supported!");
}
if(!mTempSensor.register()){
tempText.setText("\nAmbient temperature sensor not supported!");
}
if(!mAccelSensor.register()){
accelText.setText("\nAccelerometer sensor not supported!");
}
if(!mGyroSensor.register()){
gyroText.setText("\nGyroscope sensor not supported!");
}
if(!mMagSensor.register()){
magText.setText("\nMagnetometer sensor not supported!");
}
if(!mRotSensor.register()){
rotationText.setText("\nRotation Vector sensor not supported!");
}
if(!mGravitySensor.register()){
gravityText.setText("\nGravity sensor not supported!");
}
if(!mStepCounter.register()){
stepCounterText.setText("\nStep counter sensor not supported!");
}
if(!mStepDetector.register()){
stepDetectorText.setText("\nStep detector sensor not supported!");
}
if(!mLinearAcc.register()) {
linearAccText.setText("\nLinear accelerometer not supported!");
}
}
@Override
protected void onPause() {
super.onStop();
}
private void start() throws IllegalStateException, IOException {
mNoise.starter();
}
private void stop() {
// mNoise.stop();
}
private void sleep() {
// mNoise.stop();
}
private static byte[] longToByteArray(long data, int arrayLength) {
byte[] array = new byte[arrayLength];
int n = 0;
for(int i = arrayLength - 1; 0 <= i; --i) {
array[n] = (byte)(data >> i*8);
n++;
}
Log.e("SensorActivity", "longToByteArray array= " + Arrays.toString(array));
return array;
}
}