简体   繁体   中英

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. :)

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);

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM