Question

I am having an issue with detecting gestures. I get an error every time I double tap, and searching the internet for what it means seems futile - I only get solutions to particular cases which don't apply to me.

Here is the code:

package com.example.rpgengine;

import android.app.Activity;
import android.os.Bundle;
import android.view.GestureDetector;
import android.view.GestureDetector.SimpleOnGestureListener;
import android.view.Menu;
import android.view.MotionEvent;

public class MainActivity extends Activity {
    private ZoneView currentZone;
    private GestureDetector gestureDetector;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        currentZone = (ZoneView) findViewById(R.id.zoneview);
        setContentView(R.layout.activity_main);

        // initialize the GestureDetector
        gestureDetector = new GestureDetector(this, gestureListener);
    }

    @Override
    public void onPause() {
        super.onPause();
    } // end method onPause

    // release resources
    @Override
    protected void onDestroy() {
        super.onDestroy();
    } // end method onDestroy

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        // get int representing the type of action which caused this event
        int action = event.getAction();

        // the user user touched the screen or dragged along the screen
        if (action == MotionEvent.ACTION_DOWN
                || action == MotionEvent.ACTION_MOVE) {
            //currentZone.drawPath(event);
        } // end if

        // call the GestureDetector's onTouchEvent method
        return gestureDetector.onTouchEvent(event);
    } // end method onTouchEvent

    // listens forjk touch events sent to the GestureDetector
    SimpleOnGestureListener gestureListener = new SimpleOnGestureListener() {
        // called when the user double taps the screen
        @Override
        public boolean onDoubleTap(MotionEvent e) {
            currentZone.movePlayer(e); // fire the cannonball
            return true; // the event was handled
        } // end method onDoubleTap
    };

}

And here is the error:

03-28 03:19:54.165: E/InputEventReceiver(5931): Exception dispatching input event.
03-28 03:19:54.165: E/MessageQueue-JNI(5931): Exception in MessageQueue callback: handleReceiveCallback
03-28 03:19:54.165: E/MessageQueue-JNI(5931): java.lang.NullPointerException
03-28 03:19:54.165: E/MessageQueue-JNI(5931):   at com.example.rpgengine.MainActivity$1.onDoubleTap(MainActivity.java:62)
03-28 03:19:54.165: E/MessageQueue-JNI(5931):   at android.view.GestureDetector.onTouchEvent(GestureDetector.java:498)
03-28 03:19:54.165: E/MessageQueue-JNI(5931):   at com.example.rpgengine.MainActivity.onTouchEvent(MainActivity.java:54)
03-28 03:19:54.165: E/MessageQueue-JNI(5931):   at android.app.Activity.dispatchTouchEvent(Activity.java:2399)
03-28 03:19:54.165: E/MessageQueue-JNI(5931):   at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1873)
03-28 03:19:54.165: E/MessageQueue-JNI(5931):   at android.view.View.dispatchPointerEvent(View.java:7307)
03-28 03:19:54.165: E/MessageQueue-JNI(5931):   at android.view.ViewRootImpl.deliverPointerEvent(ViewRootImpl.java:3172)
03-28 03:19:54.165: E/MessageQueue-JNI(5931):   at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:3117)
03-28 03:19:54.165: E/MessageQueue-JNI(5931):   at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:4153)
03-28 03:19:54.165: E/MessageQueue-JNI(5931):   at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:4132)
03-28 03:19:54.165: E/MessageQueue-JNI(5931):   at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:4224)
03-28 03:19:54.165: E/MessageQueue-JNI(5931):   at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:171)
03-28 03:19:54.165: E/MessageQueue-JNI(5931):   at android.os.MessageQueue.nativePollOnce(Native Method)
03-28 03:19:54.165: E/MessageQueue-JNI(5931):   at android.os.MessageQueue.next(MessageQueue.java:125)
03-28 03:19:54.165: E/MessageQueue-JNI(5931):   at android.os.Looper.loop(Looper.java:124)
03-28 03:19:54.165: E/MessageQueue-JNI(5931):   at android.app.ActivityThread.main(ActivityThread.java:4745)
03-28 03:19:54.165: E/MessageQueue-JNI(5931):   at java.lang.reflect.Method.invokeNative(Native Method)
03-28 03:19:54.165: E/MessageQueue-JNI(5931):   at java.lang.reflect.Method.invoke(Method.java:511)
03-28 03:19:54.165: E/MessageQueue-JNI(5931):   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
03-28 03:19:54.165: E/MessageQueue-JNI(5931):   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
03-28 03:19:54.165: E/MessageQueue-JNI(5931):   at dalvik.system.NativeStart.main(Native Method)
03-28 03:19:54.165: D/AndroidRuntime(5931): Shutting down VM
03-28 03:19:54.165: W/dalvikvm(5931): threadid=1: thread exiting with uncaught exception (group=0x40e2c300)
03-28 03:19:54.165: E/AndroidRuntime(5931): FATAL EXCEPTION: main
03-28 03:19:54.165: E/AndroidRuntime(5931): java.lang.NullPointerException
03-28 03:19:54.165: E/AndroidRuntime(5931):     at com.example.rpgengine.MainActivity$1.onDoubleTap(MainActivity.java:62)
03-28 03:19:54.165: E/AndroidRuntime(5931):     at android.view.GestureDetector.onTouchEvent(GestureDetector.java:498)
03-28 03:19:54.165: E/AndroidRuntime(5931):     at com.example.rpgengine.MainActivity.onTouchEvent(MainActivity.java:54)
03-28 03:19:54.165: E/AndroidRuntime(5931):     at android.app.Activity.dispatchTouchEvent(Activity.java:2399)
03-28 03:19:54.165: E/AndroidRuntime(5931):     at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1873)
03-28 03:19:54.165: E/AndroidRuntime(5931):     at android.view.View.dispatchPointerEvent(View.java:7307)
03-28 03:19:54.165: E/AndroidRuntime(5931):     at android.view.ViewRootImpl.deliverPointerEvent(ViewRootImpl.java:3172)
03-28 03:19:54.165: E/AndroidRuntime(5931):     at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:3117)
03-28 03:19:54.165: E/AndroidRuntime(5931):     at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:4153)
03-28 03:19:54.165: E/AndroidRuntime(5931):     at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:4132)
03-28 03:19:54.165: E/AndroidRuntime(5931):     at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:4224)
03-28 03:19:54.165: E/AndroidRuntime(5931):     at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:171)
03-28 03:19:54.165: E/AndroidRuntime(5931):     at android.os.MessageQueue.nativePollOnce(Native Method)
03-28 03:19:54.165: E/AndroidRuntime(5931):     at android.os.MessageQueue.next(MessageQueue.java:125)
03-28 03:19:54.165: E/AndroidRuntime(5931):     at android.os.Looper.loop(Looper.java:124)
03-28 03:19:54.165: E/AndroidRuntime(5931):     at android.app.ActivityThread.main(ActivityThread.java:4745)
03-28 03:19:54.165: E/AndroidRuntime(5931):     at java.lang.reflect.Method.invokeNative(Native Method)
03-28 03:19:54.165: E/AndroidRuntime(5931):     at java.lang.reflect.Method.invoke(Method.java:511)
03-28 03:19:54.165: E/AndroidRuntime(5931):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
03-28 03:19:54.165: E/AndroidRuntime(5931):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
03-28 03:19:54.165: E/AndroidRuntime(5931):     at dalvik.system.NativeStart.main(Native Method)

I have identical code in another program (I copied it and just changed the function names that it calls) and it works. Any help would be appreciated.

Thanks!

Was it helpful?

Solution

Swap the lines below:

currentZone = (ZoneView) findViewById(R.id.zoneview);
setContentView(R.layout.activity_main);

You should initialize the view only after calling setContentView().

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top