我想在我的簡單活動中使用SensorEventListener,但每次運行應用程序後都會失敗。SensorEventListener實現不起作用
下面是活動代碼:
package com.chovanec.elastix;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
public class MainActivity extends Activity implements SensorEventListener {
private final SensorManager m_sensor_manager;
private final Sensor m_accelerometer;
public MainActivity()
{
m_sensor_manager = (SensorManager) getSystemService(SENSOR_SERVICE);
m_accelerometer = m_sensor_manager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
}
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
protected void onResume()
{
super.onResume();
m_sensor_manager.registerListener(this, m_accelerometer, SensorManager.SENSOR_DELAY_NORMAL);
}
public void onSensorChanged(SensorEvent event)
{
_("onSensorChanged");
_("" + event.values[0]);
}
public void onAccuracyChanged(Sensor sensor, int accuracy)
{
}
private void _(String msg)
{
Log.d("chovanec", msg);
}
}
這裏是目錄下載:
04-01 19:25:16.755: D/AndroidRuntime(19142): Shutting down VM
04-01 19:25:16.755: W/dalvikvm(19142): threadid=1: thread exiting with uncaught exception (group=0x40f3e2a0)
04-01 19:25:16.765: E/AndroidRuntime(19142): FATAL EXCEPTION: main
04-01 19:25:16.765: E/AndroidRuntime(19142): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.chovanec.elastix/com.chovanec.elastix.MainActivity}: java.lang.IllegalStateException: System services not available to Activities before onCreate()
04-01 19:25:16.765: E/AndroidRuntime(19142): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2024)
04-01 19:25:16.765: E/AndroidRuntime(19142): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2125)
04-01 19:25:16.765: E/AndroidRuntime(19142): at android.app.ActivityThread.access$600(ActivityThread.java:140)
04-01 19:25:16.765: E/AndroidRuntime(19142): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1227)
04-01 19:25:16.765: E/AndroidRuntime(19142): at android.os.Handler.dispatchMessage(Handler.java:99)
04-01 19:25:16.765: E/AndroidRuntime(19142): at android.os.Looper.loop(Looper.java:137)
04-01 19:25:16.765: E/AndroidRuntime(19142): at android.app.ActivityThread.main(ActivityThread.java:4898)
04-01 19:25:16.765: E/AndroidRuntime(19142): at java.lang.reflect.Method.invokeNative(Native Method)
04-01 19:25:16.765: E/AndroidRuntime(19142): at java.lang.reflect.Method.invoke(Method.java:511)
04-01 19:25:16.765: E/AndroidRuntime(19142): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1006)
04-01 19:25:16.765: E/AndroidRuntime(19142): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773)
04-01 19:25:16.765: E/AndroidRuntime(19142): at dalvik.system.NativeStart.main(Native Method)
04-01 19:25:16.765: E/AndroidRuntime(19142): Caused by: java.lang.IllegalStateException: System services not available to Activities before onCreate()
04-01 19:25:16.765: E/AndroidRuntime(19142): at android.app.Activity.getSystemService(Activity.java:4603)
04-01 19:25:16.765: E/AndroidRuntime(19142): at com.chovanec.elastix.MainActivity.<init>(MainActivity.java:19)
04-01 19:25:16.765: E/AndroidRuntime(19142): at java.lang.Class.newInstanceImpl(Native Method)
04-01 19:25:16.765: E/AndroidRuntime(19142): at java.lang.Class.newInstance(Class.java:1319)
04-01 19:25:16.765: E/AndroidRuntime(19142): at android.app.Instrumentation.newActivity(Instrumentation.java:1057)
04-01 19:25:16.765: E/AndroidRuntime(19142): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2015)
04-01 19:25:16.765: E/AndroidRuntime(19142): ... 11 more
什麼代碼必須移動到onCreate? – Michal