gpt4 book ai didi

Android 无法访问加速度计

转载 作者:行者123 更新时间:2023-11-30 04:11:50 25 4
gpt4 key购买 nike

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

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

应用很简单,就是只调用加速度计,没有这个语句就运行,上面的语句在onCreate方法之前调用,log cat在下面

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 中找出问题的原因。

最佳答案

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

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

//PLACE YOUR ACCEL CODE IN HERE

}

关于Android 无法访问加速度计,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10745039/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com