Question

I get an error trying to implement an OnTouchListener:

The method onTouch(View, MotionEvent) of type new View.OnTouchListener(){} must override a superclass method

I have no idea why it's not working as it seems I'm overrriding the method correctly:

public class MyActivity extends Activity
{
    ...creation code etc...

    OnTouchListener mTouchListener = new OnTouchListener()
    {
        @Override
        public boolean onTouch(View v, MotionEvent event)
        {
                 ...touch code...
            }
        }
}

Any ideas on how I can debug this?

Was it helpful?

Solution

In Eclipse, go to Windows>Preference>Java>Compiler and select 1.6.

You might be using 1.5, and 1.5 does not allow @Override on interfaces methods, but just on superclass method. 1.6 does.

If it still doesn't work, remove the @Override line...

public class MyActivity extends Activity
{
    ...creation code etc...

    OnTouchListener mTouchListener = new OnTouchListener()
    {

        public boolean onTouch(View v, MotionEvent event)
        {
                 ...touch code...
            }
        }
}

OTHER TIPS

View.OnTouchListener mTouchListener = new View.OnTouchListener() {

        @Override
        public boolean onTouch(View view, MotionEvent motionEvent) {
            return false; 
        }
    };

that compiles just fine, are you sure that there are no syntax errors ? What are you compiler settings ? java 5 or 6 ?

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