1

在我的 android 应用程序中,它似乎不允许我使用加速度计。当我在声明中调用它时,应用程序崩溃了。似乎导致崩溃的声明是:

SensorManager sensorManager = (SensorManager)getSystemService(Context.SENSOR_SERVICE);

应用程序很简单,只调用加速度计并在没有此语句时运行,上面的语句在 onCreate 方法之前调用,日志猫在下面

05-24 20:08:46.990: E/AndroidRuntime(1149): FATAL EXCEPTION: main
05-24 20:08:46.990: E/AndroidRuntime(1149): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.b00348312.workout/com.b00348312.workout.WorkoutAppActivity}: java.lang.IllegalStateException: System services not available to Activities before onCreate()
05-24 20:08:46.990: E/AndroidRuntime(1149):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2585)
05-24 20:08:46.990: E/AndroidRuntime(1149):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
05-24 20:08:46.990: E/AndroidRuntime(1149):     at android.app.ActivityThread.access$2300(ActivityThread.java:125)
05-24 20:08:46.990: E/AndroidRuntime(1149):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
05-24 20:08:46.990: E/AndroidRuntime(1149):     at android.os.Handler.dispatchMessage(Handler.java:99)
05-24 20:08:46.990: E/AndroidRuntime(1149):     at android.os.Looper.loop(Looper.java:123)
05-24 20:08:46.990: E/AndroidRuntime(1149):     at android.app.ActivityThread.main(ActivityThread.java:4627)
05-24 20:08:46.990: E/AndroidRuntime(1149):     at java.lang.reflect.Method.invokeNative(Native Method) 
05-24 20:08:46.990: E/AndroidRuntime(1149):     at java.lang.reflect.Method.invoke(Method.java:521)
05-24 20:08:46.990: E/AndroidRuntime(1149):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
05-24 20:08:46.990: E/AndroidRuntime(1149):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
05-24 20:08:46.990: E/AndroidRuntime(1149):     at dalvik.system.NativeStart.main(Native Method)
05-24 20:08:46.990: E/AndroidRuntime(1149): Caused by: java.lang.IllegalStateException: System services not available to Activities before onCreate()
05-24 20:08:46.990: E/AndroidRuntime(1149):     at android.app.Activity.getSystemService(Activity.java:3526)
05-24 20:08:46.990: E/AndroidRuntime(1149):     at com.b00348312.workout.WorkoutAppActivity.<init>(WorkoutAppActivity.java:25)   
05-24 20:08:46.990: E/AndroidRuntime(1149):     at java.lang.Class.newInstanceImpl(Native Method)
05-24 20:08:46.990: E/AndroidRuntime(1149):     at java.lang.Class.newInstance(Class.java:1429)
05-24 20:08:46.990: E/AndroidRuntime(1149):     at android.app.Instrumentation.newActivity(Instrumentation.java:1021) 
05-24 20:08:46.990: E/AndroidRuntime(1149):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2577)
05-24 20:08:46.990: E/AndroidRuntime(1149):     ... 11 more

当这个语句在不同的程序中工作时,我无法从 logcat 中找出这个问题的原因。

4

2 回答 2

1

这是双重否定?大声笑剪切调用系统服务的代码并粘贴到您的 onCreate

 public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

//PLACE YOUR ACCEL CODE IN HERE       

    }
于 2012-05-24T20:58:27.297 回答
0

在调用 onCreate 之后,您必须获取 SensorManager。

“系统服务在 onCreate() 之前对活动不可用”

于 2012-05-24T20:50:04.003 回答