问题
I'm working on an android application that gets sensor values from android phones in order to consume these values by a web service that stores them in a database . I don't have a real device for testing the sensors values so I used the sensor simulator [http://code.google.com/p/openintents/wiki/SensorSimulator][1] the problem now is that everytime I run the activity I get errors and here is the code of the activity (I'm using the API 15 Google APIs platform : 4.0.3)
import org.openintents.sensorsimulator.hardware.Sensor;
import org.openintents.sensorsimulator.hardware.SensorEvent;
import org.openintents.sensorsimulator.hardware.SensorEventListener;
import org.openintents.sensorsimulator.hardware.SensorManagerSimulator;
public class SimulationActivity extends Activity implements SensorEventListener {
private SensorManagerSimulator mSensorManager;
private Sensor mTemperature;
private TextView mTemperatureLabel;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mTemperatureLabel = (TextView) findViewById(R.id.temperature);
mSensorManager = SensorManagerSimulator.getSystemService(this,SENSOR_SERVICE);
mSensorManager.connectSimulator();
}
@Override
protected void onResume() {
super.onResume();
mSensorManager.registerListener(this,
mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
SensorManager.SENSOR_DELAY_FASTEST);
mSensorManager.registerListener(this,
mSensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD),
SensorManager.SENSOR_DELAY_FASTEST);
mSensorManager.registerListener(this,
mSensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION),
SensorManager.SENSOR_DELAY_FASTEST);
mSensorManager.registerListener(this,
mSensorManager.getDefaultSensor(Sensor.TYPE_TEMPERATURE),
SensorManager.SENSOR_DELAY_FASTEST);
}
@Override
protected void onStop() {
mSensorManager.unregisterListener(this);
super.onStop();
}
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
public void onSensorChanged(SensorEvent event) {
switch (event.type) {
case Sensor.TYPE_ORIENTATION: {
Log.i("Sensor changed ", " Orientation ");
mTemperatureLabel.setText("orientation");
}
break;
case Sensor.TYPE_LIGHT: {
Log.i("Sensor changed ", " light ");
mTemperatureLabel.setText("light");
}
break;
case Sensor.TYPE_TEMPERATURE: {
Log.i("Sensor changed ", " temperature ");
mTemperatureLabel.setText("temperature");
}
break;
case Sensor.TYPE_PRESSURE: {
Log.i("Sensor changed ", " pressure ");
mTemperatureLabel.setText("pressure ");
}
break;
}
}
}
and here is the errors messages
11-14 18:49:57.123: E/AndroidRuntime(4351): FATAL EXCEPTION: main
11-14 18:49:57.123: E/AndroidRuntime(4351): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.tunisietelecom.android/com.tunisietelecom.android.SimulationActivity}: java.lang.ClassNotFoundException: com.tunisietelecom.android.SimulationActivity
11-14 18:49:57.123: E/AndroidRuntime(4351): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1880)
11-14 18:49:57.123: E/AndroidRuntime(4351): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
11-14 18:49:57.123: E/AndroidRuntime(4351): at android.app.ActivityThread.access$600(ActivityThread.java:123)
11-14 18:49:57.123: E/AndroidRuntime(4351): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
11-14 18:49:57.123: E/AndroidRuntime(4351): at android.os.Handler.dispatchMessage(Handler.java:99)
11-14 18:49:57.123: E/AndroidRuntime(4351): at android.os.Looper.loop(Looper.java:137)
11-14 18:49:57.123: E/AndroidRuntime(4351): at android.app.ActivityThread.main(ActivityThread.java:4424)
11-14 18:49:57.123: E/AndroidRuntime(4351): at java.lang.reflect.Method.invokeNative(Native Method)
11-14 18:49:57.123: E/AndroidRuntime(4351): at java.lang.reflect.Method.invoke(Method.java:511)
11-14 18:49:57.123: E/AndroidRuntime(4351): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
11-14 18:49:57.123: E/AndroidRuntime(4351): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
11-14 18:49:57.123: E/AndroidRuntime(4351): at dalvik.system.NativeStart.main(Native Method)
11-14 18:49:57.123: E/AndroidRuntime(4351): Caused by: java.lang.ClassNotFoundException: com.tunisietelecom.android.SimulationActivity
11-14 18:49:57.123: E/AndroidRuntime(4351): at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:61)
11-14 18:49:57.123: E/AndroidRuntime(4351): at java.lang.ClassLoader.loadClass(ClassLoader.java:501)
11-14 18:49:57.123: E/AndroidRuntime(4351): at java.lang.ClassLoader.loadClass(ClassLoader.java:461)
11-14 18:49:57.123: E/AndroidRuntime(4351): at android.app.Instrumentation.newActivity(Instrumentation.java:1023)
11-14 18:49:57.123: E/AndroidRuntime(4351): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1871)
11-14 18:49:57.123: E/AndroidRuntime(4351): ... 11 more
回答1:
I solved this problem. When you add a library to your classpath, the library is not exported to the phone. You need to go to Proyect Properties -> Java Build Path -> Order and Export and "tick" the library you need to be exported to the phone. If you don't do this, the library won’t be on the phone and Android won’t find it!!!
回答2:
Caused by: java.lang.ClassNotFoundException: com.tunisietelecom.android.SimulationActivity
Have you declared your Activity in the manifest file?
来源:https://stackoverflow.com/questions/13385345/how-to-work-with-android-and-sensorsimulator-to-get-sensors-values-and-store-the