Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wrong 1st argument type

I am relatively new to Java and Android development, so I am sorry for disturbing you. I want to create fade-in animation on long click using AnimationUtils.LoadAnimation(), but I am facing error:

Wrong 1st argument type. Found: 'android.view.View.OnLongClickListener', required: 'android.content.Context'

This is my code:

  BasicsButton.setOnLongClickListener(new View.OnLongClickListener() {
        @Override
        public boolean onLongClick(View v) {

            Vibrator vib = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                vib.vibrate(VibrationEffect.createOneShot(500,VibrationEffect.DEFAULT_AMPLITUDE));
            }else{
                vib.vibrate(500);
            }
                Animation in = AnimationUtils.loadAnimation(this, R.anim.fadein);
                blurView.startAnimation(in);
                blurView.setVisibility(View.VISIBLE);
            return true;
        }

I am unaware of what is wrong, this example seems to work, but not for me.

Thank you in advance. :)

like image 685
Krum Cvetkov Avatar asked Mar 07 '23 00:03

Krum Cvetkov


2 Answers

The problem is following line

Animation in = AnimationUtils.loadAnimation(this, R.anim.fadein);

Since the above method is call in anonymous class, this refers to OnLongClickListener rather than Activity.

Change it to as follows:

Animation in = AnimationUtils.loadAnimation(<ActivityName>.this, R.anim.fadein);

like image 150
Sagar Avatar answered Mar 12 '23 09:03

Sagar


if you use this code inside a fragment, use getContext() instead of this.

Have you tried the below code:

  Vibrator vib = (Vibrator) getContext().getSystemService(Context.VIBRATOR_SERVICE);
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
               vib.vibrate(VibrationEffect.createOneShot(500,VibrationEffect.DEFAULT_AMPLITUDE));
            }else{
                vib.vibrate(500);
            }
            Animation in = AnimationUtils.loadAnimation(getContext(), R.anim.slide_in_left);
            blurView.startAnimation(in);
            blurView.setVisibility(View.VISIBLE);
like image 40
Farrokh Shahriari Avatar answered Mar 12 '23 09:03

Farrokh Shahriari