Home > Net >  OnLongClickListener() error Method does not override method from its superclass
OnLongClickListener() error Method does not override method from its superclass

Time:07-29

Idk what to do so basically i was creating a button which opens new activity on long press could u fix this?

  @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    button = (Button) findViewById(R.id.button);

    defineView();
    handleIntent();
    defineActionBar();
    checkPermission();

    button.setOnLongClickListener(new View.OnCLongClickListener() {
        @Override
        public void onLongClick(View v) {
            openWebsite();
        }
    });
}



public void openWebsite() {
    Intent intent = new Intent(this, Website.class);
    startActivity(intent);
}

CodePudding user response:

onLongClick returns a boolean, not a void. Change that and make it return true (assuming you want to consume the touch).

CodePudding user response:

I guessed you have typed it manually. I found ..View.OnCLongClickListener() .. which should be View.OnLongClickListener() without 'C', you may try using the suggestion popping up while typing by the IDE directly, it will reduce our typo mistake. Another one, the method inside the LongClickListener should be 'boolean' instead of 'void', which will return either TRUE or FALSE. True, means that your event will just be handled within that code, no other events will be fired after this point, false otherwise.

I provided the code as below, you may refer to it and try it out. Hope it works for you. :)

button.setOnLongClickListener(new OnLongClickListener() {
    public boolean onLongClick(View v) {
        openWebsite();
        return true;
    }
});

Happy coding!! :D

  • Related