在谷歌的官网上有一段重力感应示例代码,但我复制下来运行时却出现了错误,源代码如下:package com.sensor.test2;import android.app.Activity;
import android.os.Bundle;
import android.hardware.SensorManager;
import android.hardware.Sensor;
import android.hardware.SensorEventListener;
import android.hardware.SensorEvent;public class SensorTest extends Activity implements SensorEventListener {
private final SensorManager mSensorManager;
private final Sensor mAccelerometer; public SensorTest() {
mSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
mAccelerometer = mSensorManager
.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
} protected void onResume() {
super.onResume();
mSensorManager.registerListener(this, mAccelerometer,
SensorManager.SENSOR_DELAY_NORMAL);
} protected void onPause() {
super.onPause();
mSensorManager.unregisterListener(this);
} public void onAccuracyChanged(Sensor sensor, int accuracy) {
} public void onSensorChanged(SensorEvent event) {
}
}出错的提示是这样的:
07-05 12:15:28.235: W/dalvikvm(11836): threadid=3: thread exiting with uncaught exception (group=0x40026268)
07-05 12:15:28.245: E/AndroidRuntime(11836): Uncaught handler: thread main exiting due to uncaught exception
07-05 12:15:28.265: E/AndroidRuntime(11836): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.sensor.test2/com.sensor.test2.SensorTest}: java.lang.IllegalStateException: System services not available to Activities before onCreate()
07-05 12:15:28.265: E/AndroidRuntime(11836):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2417)
07-05 12:15:28.265: E/AndroidRuntime(11836):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512)
07-05 12:15:28.265: E/AndroidRuntime(11836):  at android.app.ActivityThread.access$2200(ActivityThread.java:119)
07-05 12:15:28.265: E/AndroidRuntime(11836):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1863)
07-05 12:15:28.265: E/AndroidRuntime(11836):  at android.os.Handler.dispatchMessage(Handler.java:99)
07-05 12:15:28.265: E/AndroidRuntime(11836):  at android.os.Looper.loop(Looper.java:123)
07-05 12:15:28.265: E/AndroidRuntime(11836):  at android.app.ActivityThread.main(ActivityThread.java:4363)
07-05 12:15:28.265: E/AndroidRuntime(11836):  at java.lang.reflect.Method.invokeNative(Native Method)
07-05 12:15:28.265: E/AndroidRuntime(11836):  at java.lang.reflect.Method.invoke(Method.java:521)
07-05 12:15:28.265: E/AndroidRuntime(11836):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
07-05 12:15:28.265: E/AndroidRuntime(11836):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
07-05 12:15:28.265: E/AndroidRuntime(11836):  at dalvik.system.NativeStart.main(Native Method)
07-05 12:15:28.265: E/AndroidRuntime(11836): Caused by: java.lang.IllegalStateException: System services not available to Activities before onCreate()
07-05 12:15:28.265: E/AndroidRuntime(11836):  at android.app.Activity.getSystemService(Activity.java:3468)
07-05 12:15:28.265: E/AndroidRuntime(11836):  at com.sensor.test2.SensorTest.<init>(SensorTest.java:15)
07-05 12:15:28.265: E/AndroidRuntime(11836):  at java.lang.Class.newInstanceImpl(Native Method)
07-05 12:15:28.265: E/AndroidRuntime(11836):  at java.lang.Class.newInstance(Class.java:1479)
07-05 12:15:28.265: E/AndroidRuntime(11836):  at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
07-05 12:15:28.265: E/AndroidRuntime(11836):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2409)
07-05 12:15:28.265: E/AndroidRuntime(11836):  ... 11 more请问这是怎么回事的呢?不仅是官网给出的代码,我还试了CSDN和其他社区中给出的相关代码,都是同样的错误~
最近刚开始学Android开发,有些东西还不太明白,求详解,谢谢!

解决方案 »

  1.   

    Activity是有自己的生命周期的,你的SensorTest()构造方法明显不对,把SensorTest()里面的代码放在onCreate()里面就可以了。
      

  2.   

    OnCreate 之后才能getSystemService
      

  3.   

        public Object getSystemService(String name) {
            if (getBaseContext() == null) {
                throw new IllegalStateException(
                        "System services not available to Activities before onCreate()");
            }        if (WINDOW_SERVICE.equals(name)) {
                return mWindowManager;
            } else if (SEARCH_SERVICE.equals(name)) {
                ensureSearchManager();
                return mSearchManager;
            }
            return super.getSystemService(name);
        }