让我的 Android 应用通过 wasItTouched 事件振动

Getting my Android App to vibrate with a wasItTouched event

我有一个应用程序,其中有一些对象(精灵)在屏幕上移动。我希望 phone 在 touched/clicked 开启时振动,但我遇到了一些麻烦。

我所做的是添加我认为可以在 wasItTouched 方法(在 sprite class 中)中起作用的内容,如下所示:

public boolean wasItTouched(float ex, float ey) {
    boolean touched = false;
    if ((x <= ex) && (ex < x + bmp_width) &&
            (y <= ey) && (ey < y + bmp_height)) {
        touched = true;

        Vibrator vibe = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
        vibe.vibrate(500);
        //mySound.play(zapSoundId, 1, 1, 1, 0, 1);


    }

    return touched;
}

正如您可能从 'comments' 中看到的那样,我尝试添加声音但也没有用。

我想知道我是否没有把它放在正确的地方。

我已经在清单中声明了它(见下文):

<uses-permission android:name="android.permission.VIBRATE" />

当我点击 sprite(或屏幕上的任何地方)时,应用程序崩溃了。

下面是我从 logcat 得到的:

04-24 17:32:56.192 7551-7551/cct.mad.lab D/AndroidRuntime: Shutting down VM
04-24 17:32:56.192 7551-7551/cct.mad.lab W/dalvikvm: threadid=1: thread exiting with uncaught exception (group=0xb0d7cb20)
04-24 17:32:56.192 7551-7551/cct.mad.lab E/AndroidRuntime: FATAL EXCEPTION: main
                                                       Process: cct.mad.lab, 
PID: 7551
                                                       java.lang.IllegalStateException: System services not available to Activities before onCreate()
                                                           at android.app.Activity.getSystemService(Activity.java:4532)
                                                           at cct.mad.lab.Sprite.wasItTouched(Sprite.java:136)
                                                           at cct.mad.lab.GameView.onTouchEvent(GameView.java:187)
                                                           at android.view.View.dispatchTouchEvent(View.java:7706)
                                                           at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216)
                                                           at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1917)
                                                           at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216)
                                                           at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1917)
                                                           at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216)
                                                           at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1917)
                                                           at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216)
                                                           at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1917)
                                                           at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:2068)
                                                           at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1515)
                                                           at android.app.Activity.dispatchTouchEvent(Activity.java:2458)
                                                           at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:2016)
                                                           at android.view.View.dispatchPointerEvent(View.java:7886)
                                                           at android.view.ViewRootImpl$ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:3954)
                                                           at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:3833)
                                                           at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3399)
                                                           at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3449)
                                                           at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3418)
                                                           at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:3525)
                                                           at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3426)
                                                           at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:3582)
                                                           at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3399)
                                                           at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3449)
                                                           at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3418)
                                                           at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3426)
                                                           at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3399)
                                                           at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:5602)
                                                           at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:5582)
                                                           at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:5553)
                                                           at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:5682)
                                                           at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:185)
                                                           at android.os.MessageQueue.nativePollOnce(Native Method)
                                                           at android.os.MessageQueue.next(MessageQueue.java:138)
                                                           at android.os.Looper.loop(Looper.java:123)
                                                           at android.app.ActivityThread.main(ActivityThread.java:5017)
                                                           at java.lang.reflect.Method.invokeNative(Native Method)
                                                           at java.lang.reflect.Method.invoke(Method.java:515)
                                                           at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
                                                           at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
                                                           at dalvik.system.NativeStart.main(Native Method)
04-24 17:32:56.192 1591-1711/system_process W/ActivityManager:   Force finishing activity cct.mad.lab/.GameActivity

如果有人能提供任何指导,我将不胜感激。

谢谢

"System services not available to Activities before onCreate()"

您在 activity 上调用 onCreate 之前以某种方式调用它,这是不合法的。发生的主要情况是,如果您在对象的 init 中调用一个函数作为默认值,或者您尝试通过 new 创建一个 Activity(永远不应该这样做)。