Ich habe eine Klasse mit Sensormanager und immer an der Linie gescheitertFehler Androids Sensormanager mit
sensorManager = (SensorManager) getSystemService(Activity.SENSOR_SERVICE);
Meine ganze Klasse ist
public class AcceSensorInterface extends AppCompatActivity implements SensorEventListener {
private SensorManager sensorManager;
private Sensor accelerometer;
public Queue<Float> queuex;
public Queue<Float> queuey;
public Queue<Float> queuez;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
public void createSensorManager(){
sensorManager = (SensorManager) getSystemService(Activity.SENSOR_SERVICE);
if (sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER) != null) {
// success! we have an accelerometer
accelerometer = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
sensorManager.registerListener(this, accelerometer, SensorManager.SENSOR_DELAY_NORMAL);
// vibrateThreshold = accelerometer.getMaximumRange()/2;
} else {
// fai! we dont have an accelerometer!
}
queuex = new ConcurrentLinkedQueue<Float>();
queuey = new ConcurrentLinkedQueue<Float>();
queuez = new ConcurrentLinkedQueue<Float>();
}
@Override
public void onSensorChanged(SensorEvent event) {
queuex.add(Float.valueOf(event.values[0]));
queuey.add(Float.valueOf(event.values[1]));
queuez.add(Float.valueOf(event.values[2]));
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
public void deleteQueues(){
sensorManager.unregisterListener(this);
queuex.clear();
queuey.clear();
queuez.clear();
}
}
Was könnte falsch sein?
EDIT: Das Fehlerprotokoll ist
04-01 18:03:52.025 31752-31752/com.abbott.forcetest E/AndroidRuntime: FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to resume activity {com.forcetest/com.forcetest.MainActivity}: java.lang.IllegalStateException: System services not available to Activities before onCreate()
at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2616)
at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2644)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1269)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4898)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1008)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:775)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.IllegalStateException: System services not available to Activities before onCreate()
at android.app.Activity.getSystemService(Activity.java:4603)
at com.abbott.forcetest.AcceInterface.createSensor(AcceInterface.java:25)
at com.abbott.forcetest.RawData.begin_process(RawData.java:277)
at com.abbott.forcetest.RawData.onResume(RawData.java:211)
at android.support.v4.app.Fragment.performResume(Fragment.java:2020)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1107)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1252)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1234)
at android.support.v4.app.FragmentManagerImpl.dispatchResume(FragmentManager.java:2056)
at android.support.v4.app.FragmentController.dispatchResume(FragmentController.java:196)
at android.support.v4.app.FragmentActivity.onResumeFragments(FragmentActivity.java:505)
at android.support.v4.app.FragmentActivity.onPostResume(FragmentActivity.java:494)
at android.support.v7.app.AppCompatActivity.onPostResume(AppCompatActivity.java:161)
at android.app.Activity.performResume(Activity.java:5293)
at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2606)
at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2644)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1269)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4898)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1008)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:775)
at dalvik.system.NativeStart.main(Native Method)
Dank
Welches Telefon verwenden Sie? Viele Telefone haben keinen Sensor – Saveen
Was meinst du mit scheitern? Gibt die Funktion null zurück? Stürzt Ihre Anwendung ab? Wenn ja, poste bitte den Stacktrace – 0xDEADC0DE
SAMSUNG. Got accelerometer – batuman